编写一个学生类

来源:互联网 发布:深圳盘古数据有限公司 编辑:程序博客网 时间:2024/05/29 15:36

(1)

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("学号出错,默认用10代替");
no = 10;}
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("年龄出错,使用默认年龄18岁代替");
age = 18;}
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(-18);
p1.setSex("女");
p1.setNo(-3);
p1.getName();
p1.getAge();
p1.getSex();
p1.getNo();
}
}

(2)

public class Student2 {
public static void main(String[] args) {
Student p2 = new Student();
p2.setName("唉");
p2.setSex("男");
p2.setNo(3);
p2.getName();
p2.getSex();
p2.getNo();
}
}

(3)

public class Student3 {
public static void main(String[] args) {
Student p3 = new Student();
p3.setName("毁");
p3.setNo(9);
p3.getName();
p3.getNo();
}
}


(4)

public class Student4 {
public static void main(String[] args) {
Student p4 = new Student();
p4.setNo(16);
p4.getNo();
}
}


(1)


(2)


(3)


(4)


阅读全文
0 0
原创粉丝点击