zoukankan      html  css  js  c++  java
  • 理解Java的Class类、"this."关键字、Constructor构造器(一)

    import java.util.*;
    
    public class BookTest
    {
    	public static void main(String[] args)
    	{
    		//Book book = new Book("A Tale of Two Cities", 1895);
    		Book book = new Book("A Tale of Two Cities");
    		System.out.println(book.title);
    		System.out.println(book.pubYear);
    		System.out.println(Book.count);
    		Book book2 = new Book("War and Peace");
    		System.out.println(Book.count); // count 是静态变量,新建book2实类后自动+1
    	}
    }
    
    class Book
    {
    	String title;
    	int pubYear;
    	static int count = 0;
    	public Book(String title)
    	{
    		this(title, -1); //如果没有接收到第二个变量,pubYear自动设为1	
    	}
    
    	public Book(String title, int pubYear)
    	{
    		setTitle(title);
    		setPubYear(pubYear);
    		++count;
    	}
    	public void setTitle(String title)
    	{
    		this.title = title;			
    	}
    
    
    	public void setPubYear(int pubYear)
    	{
    		this.pubYear = pubYear;			
    	}
    
    }
    
  • 相关阅读:
    numpy常用函数
    python 语法学习
    python学习:字典
    python 字符串使用
    k-近邻算法
    Numpy函数库
    机器学习初体验
    Xcode8 + iOS10Beta 权限问题崩溃的解决方法
    苹果设备全攻略
    使用 Xcode 代码块
  • 原文地址:https://www.cnblogs.com/yaos/p/14014305.html
Copyright © 2011-2022 走看看