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;			
    	}
    
    }
    
  • 相关阅读:
    JavaScript中的数组
    JavaScript中的对象
    Highcharts中设置x轴为时间的写法
    CSS 选择器(基础)
    DOM节点
    平衡木蜻蜓
    python2.7 qt4
    C语言优先级
    树莓派与stm32通信
    linux下USB转串口配置
  • 原文地址:https://www.cnblogs.com/yaos/p/14014305.html
Copyright © 2011-2022 走看看