編寫一個學生類Students,該類成員變數包括學號no、姓名name、性別sex和年齡age,該類的成員方法有genNo()getName ). * getSex().getAge().和setA
阿新 • • 發佈:2019-02-19
/*編寫一個學生類Students,該類成員變數包括學號no、姓名name、性別sex和年齡age,該類的成員方法有genNo()getName ).
* getSex().getAge().和setAge() 新增構造方法為所有成員變數賦初值,構造方法要有4種格式:
(1) 包括no,name,sex 和age四個引數。
(2) 包括 no.name 和sex 三個引數。
(3) 包括mo和name兩個引數。
(4) 只包括no一個引數。
*/
class Student {// 建立Student這個類
private int no;// 成員變數
private static int age;
private String name;
private String sex;
public void setAge(int myAge) {//封裝
if (myAge <= 0) {
System.out.println("年齡出錯,使用預設年齡18歲代替");
age=18;
}
else
age=myAge;
}
public int getNo() {
System.out.println("學號:"+no);
return no;
}
public String getName(){
System.out.println("姓名:"+name);
return name;}
public String getSex(){
System.out.println("性別:"+sex);
return sex;}
public int getAge() {
System.out.println("年齡:"+age);
return age;
}
public Student(int myNo, String myName, String mySex, int myAge) {//構造方法變數初始化
no = myNo;
name = myName;
sex = mySex;
age = myAge;
}
public Student(int myNo, String myName, String mySex) {
no = myNo;
name = myName;
sex = mySex;
}
public Student(int myNo, String myName) {
no = myNo;
name = myName;
}
public Student(int myNo) {
no = myNo;
}
public static void main(String[] args) { // 宣告物件和建立物件
Student Student1= new Student(1625,"張三","m",20);
Student Student2= new Student(1624,"李四","f");
Student Student3= new Student(1623,"張三");
Student Student4= new Student(1622);
Student1.setAge(age);
Student1.getNo();
Student1.getName();
Student1.getSex();
Student1.getAge();
Student2.getNo();
Student2.getName();
Student2.getSex();
Student3.getNo();
Student3.getName();
Student4.getNo();
}
}
* getSex().getAge().和setAge() 新增構造方法為所有成員變數賦初值,構造方法要有4種格式:
(1) 包括no,name,sex 和age四個引數。
(2) 包括 no.name 和sex 三個引數。
(3) 包括mo和name兩個引數。
(4) 只包括no一個引數。
*/
class Student {// 建立Student這個類
private int no;// 成員變數
private static int age;
private String name;
private String sex;
public void setAge(int myAge) {//封裝
if (myAge <= 0) {
System.out.println("年齡出錯,使用預設年齡18歲代替");
age=18;
}
else
age=myAge;
}
public int getNo() {
System.out.println("學號:"+no);
return no;
}
public String getName(){
System.out.println("姓名:"+name);
return name;}
public String getSex(){
System.out.println("性別:"+sex);
return sex;}
public int getAge() {
System.out.println("年齡:"+age);
return age;
}
public Student(int myNo, String myName, String mySex, int myAge) {//構造方法變數初始化
no = myNo;
name = myName;
sex = mySex;
age = myAge;
}
public Student(int myNo, String myName, String mySex) {
no = myNo;
name = myName;
sex = mySex;
}
public Student(int myNo, String myName) {
no = myNo;
name = myName;
}
public Student(int myNo) {
no = myNo;
}
public static void main(String[] args) { // 宣告物件和建立物件
Student Student1= new Student(1625,"張三","m",20);
Student Student2= new Student(1624,"李四","f");
Student Student3= new Student(1623,"張三");
Student Student4= new Student(1622);
Student1.setAge(age);
Student1.getNo();
Student1.getName();
Student1.getSex();
Student1.getAge();
Student2.getNo();
Student2.getName();
Student2.getSex();
Student3.getNo();
Student3.getName();
Student4.getNo();
}
}