理解Java的Class类、"this."关键字、Constructor构造器(一)
2024-10-07 16:03:37
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;
}
}
最新文章
- Java-Android【1】-控制手机震动
- SQL Server2008 表旋转(pivot)技术
- 使用Atlas实现MySQL读写分离
- 《OpenCV入门》(三)
- Object Pascal 语法之异常处理
- Objective-C的对象模型和runtime机制
- BestCoder Round #68 (div.2) 1002 tree
- An error occurred while filtering resources
- Linux下创建、查看、提取和修改静态库(*.a)
- spring xml记录
- 开心系列学习笔记---------javascript(7)
- NET Core开发-使用Nancy框架
- 控制执行流程——(Java学习笔记三)
- LeetCode Weekly Contest
- Akka(8): 分布式运算:Remoting-远程查找式
- Hive入门教程
- c中有序表的简单定义
- vue实现打印功能的两种方法
- 【Diary】期中考+春游记
- oracle 数据库去重复数据
热门文章
- ";sorted()";中的";Key Functions";
- JavaWeb(六):会话与状态管理
- 【leetcode】336. Palindrome Pairs
- leetcode-167周赛-1291-顺次数
- DELPHI FMX IOS模拟器调试时出现No SDKs could be found
- 【HDOJ6606】Distribution of books(二分,BIT)
- 转载:IDEA lombok插件的安装和使用
- 北风设计模式课程---20、UML类图介绍
- DB-概念-同义词:同义词/Synonym
- Chrome-逆向分析JS-2获取发送请求位置(以datatables获取表格数据为例)