编写一个学生类

(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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值