/**
* 构造方法
* toString()
* Getters and Setters
*/
1.构造方法
1.1 属性(成员变量,全局变量,在方法里面叫局部变量):是描述当前对象的特征,一般用 private 修饰
private String cId;
private String cName;
private String cColor;
1.2 方法:是描述当前对象的行为
public void show() {
System.out.print("卡号:" + cId);
System.out.print("\t卡名:" + cName);
System.out.println("\t卡色:" + cColor);
}
1.3 构造方法(与类名同名且没有返回值):初始化成员变量,也就是给成员变量赋值
public class BankCard {
private String cId;
private String cName;
private String cColor;
public BankCard(String cId, String cName, String cColor) {
//this 代表 BankCard 这个本类
this.cId = cId;
this.cName = cName;
this.cColor = cColor;
}
}
//在主方法内使用构造方法初始化对象
public class Test {
public static void main(String[] args) {
//初始化一个 BankCard 对象
//new 就是实例化,就是调用构造方法
BankCard bc = n
Java笔记(构造方法、toString()、Getters and Setters)
最新推荐文章于 2023-10-27 09:34:00 发布