给成员变量赋值的方式:
A:无参构造方法+setXxx()
B:带参构造方法
class Student {
private String name;
private int age;
public Student() {}
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void show() {
System.out.println(name+"---"+age);
}
}
class StudentDemo {
public static void main(String[] args) {
//无参+set
Student s1 = new Student();
s1.setName("喻永星");
s1.setAge(25);
s1.show();
//带参
Student s2 = new Student("黑马云",11);
s2.show();
}
} |
|