1. 程式人生 > >題目: 編寫一個學生類Students,該類成員變數包括學號no、姓名name、性別sex和年齡age

題目: 編寫一個學生類Students,該類成員變數包括學號no、姓名name、性別sex和年齡age

題目:

編寫一個學生類Students,該類成員變數包括學號no、姓名name、性別sex和年齡age

class Student{
private int no;
private String name;
private String sex;
private int age;
public void setNo(int myNo){
if(myNo <=0||myNo>=50){
System.out.println("學號出錯,預設用00代替");
no = 00;}
else
no = myNo;
}
public void setSex(String mySex){
sex = mySex;
}
public void setName(String myName){
name = myName;
}
public void setAge(int myAge){
if(myAge <=0){
System.out.println("年齡出錯,使用預設年齡20歲代替");
age = 20;}
else
age = myAge;
}


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 int getNo(){
System.out.println("學號:"+no);
return no;}
}
public class Student1 {
public static void main(String[] args) {
Student p1 = new Student();
p1.setName("張三");
p1.setAge(-20);
p1.setSex("男");
p1.setNo(01);
p1.getName();
p1.getAge();
p1.getSex();
p1.getNo();
}


}