Java经典编程题
【摘要】
Java编程题 1.设计一个学生类Student和它的一个子类Undergraduate. 要求如下: (1) Student 类有name和age属性,一个包含两个参数的构造方法,用于给name和age...
Java编程题
1.设计一个学生类Student和它的一个子类Undergraduate. 要求如下:
(1) Student 类有name和age属性,一个包含两个参数的构造方法,用于给name和age属性赋值,一个show()方法打印Student的属性信息。(将name和age属性私有化)
(2)本科生类Undergraduate增加一个degree(学位)属性。有一个包含三个参数的构造方法,前两个参数用于给继承的name和age属性赋值,第三个参数给degree专业赋值,一个show()方法用于打印Undergraduate的属性信息。( degree属性私有化)
(3)在测试类中分别创建Student对象和Undergraduate对象,调用它们的show()。
package kcy;
public class main {
public static void main(String[] args) {
Student a= new Student("李四",19);
a.show();
System.out.println("---------------------------------------");
Undergratude a1=new Undergratude("张三",19,"在读本科生");
a1.show();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
package kcy;
public class Student {
private String name;
private int age;
public Student(String name, int age)
{ this.name = name;
this.age = age; }
public void show(){ System.out.println("name="+getName()+"\nage="+getAge()); }
public String getName()
{ return name; }
public void setName(String name)
{ this.name = name; }
public int getAge()
{ return age; }
public void setAge(int age)
{ this.age = age; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
package kcy;
public class Undergratude extends Student {
private String degree;
public Undergratude (String name,int age,String degree) {
super(name,age);
this.degree = degree;
}
public void show(){
System.out.println("name="+getName()+"\nage="+getAge()+"\ndegree="+getDegree());
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
文章来源: blog.csdn.net,作者:静Yu,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_46285118/article/details/109057128
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)