《super class》
public class Card
{
private String name;
public Card()
{
name = "";
}
public Card(String n)
{
name = n;
}
public String getName()
{
return name;
}
public boolean isExpired()
{
return false;
}
public String toString()
{
return "Card holder: " + name;
}
}
子类
public class IDCard extends Card{
private String idnum;
{
}
public class DriverLicense extends IDCard{
private String year;
}
要求以如下形式为IDCard、DriverLicense添加构造函数,每个构造函数应该调用超类构造函数来设置名称
public IDCard(String n, String id)
{
super(n);
idnum = id;
}
如何实现?
Java类的继承
答案:2 悬赏:70 手机版
解决时间 2021-12-22 16:41
- 提问者网友:最爱你的唇
- 2021-12-21 19:28
最佳答案
- 五星知识达人网友:街头电车
- 2021-12-21 20:20
public class Card {
private String name;
public Card() {
name = "";
}
public Card(String n) {
name = n;
}
public String getName() {
return name;
}
public boolean isExpired() {
return false;
}
@Override
public String toString() {
return "Card holder: " + name;
}
}
// 子类
class IDCard extends Card {
private String idnum;
public IDCard(String n, String id){
super(n);
this.idnum = id;
}
}
class DriverLicense extends IDCard {
private String year;
public DriverLicense(String n, String id, String year) {
super(n, id);
this.year = year;
}
}
private String name;
public Card() {
name = "";
}
public Card(String n) {
name = n;
}
public String getName() {
return name;
}
public boolean isExpired() {
return false;
}
@Override
public String toString() {
return "Card holder: " + name;
}
}
// 子类
class IDCard extends Card {
private String idnum;
public IDCard(String n, String id){
super(n);
this.idnum = id;
}
}
class DriverLicense extends IDCard {
private String year;
public DriverLicense(String n, String id, String year) {
super(n, id);
this.year = year;
}
}
全部回答
- 1楼网友:神也偏爱
- 2021-12-21 21:52
//c1.java
classpersona{
privatestringname;
publicvoidsetname(stringnewname){
name=newname;
}
publicstringgetname(){
returnname;
}
}
classstudentaextendspersona{
privatestringdepartment;
publicvoidsetdepartment(stringnewdepartment){
department=newdepartment;
}
publicstringgetdepartment(){
returndepartment;
}
}
classc1{
publicstaticvoidmain(stringargs[]){
studentas1=newstudenta();
s1.setname("张三");
s1.setdepartment("计算机系");
system.out.println("你好,我是"+s1.getname());//你这里写错了不是setname
system.out.println("我是"+s1.getdepartment()+"的学生");//不是setdepartment
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯