package lei;
public class Student {
private String name;
private int age;
//無參數的構造方法
public Student() {//上方菜單code——Generate-constructor-select note
System.out.println("無參數構造方法");
}
//全參數的構造方法
public Student(String name, int age) {//上方菜單code——Generate-constructor-選擇成員變量-ok
System.out.println("全參數構造方法");
this.name = name;
this.age = age;
}
public String getName() {//上方菜單code——Generate-getter and setter-選擇成員變量-ok
return name;
}
public void setName(String name) {//上方菜單code——Generate-getter and setter-選擇成員變量-ok
this.name = name;
}
public int getAge() {//上方菜單code——Generate-getter and setter-選擇成員變量-ok
return age;
}
public void setAge(int age) {//上方菜單code——Generate-getter and setter-選擇成員變量-ok
this.age = age;
}
}
=======================================================================================
package lei;
public class Student_yong {
public static void main(String[] args) {
Student stu=new Student();//直接調用無參數構造方法
stu.setAge(18);
stu.setName("豬八戒");
System.out.println("名字:"+stu.getName()+" 年齡:"+stu.getAge());
Student stu1=new Student("孫悟空",34);
System.out.println("名字:"+stu1.getName()+" 年齡:"+stu1.getAge());
}
}