java 关于子类怎么添加自己新的属性?
abstractclassPerson{Stringname;intage;Stringprofessional;Person(Stringname,intage,Str...
abstract class Person {
String name;
int age;
String professional;
Person(String name,int age,String professional) {
this.name= name;
this.age = age;
this.professional=professional;
}
public abstract void display();
}
class Students extends Person {
Students(String n,int a,String p) {
super(n,a,p);
}
public void display() {
System.out.println("我的姓名="+ name + " " + "我的年龄="+age +" " +"我的专业="+professional);
}
}
public class Test5 {
public static void main(String[] args) {
Person p = new Students("男",19,"美术");
Person o = new Students("女",14,"跳舞");
p.display();
o.display();
}
}
这个是我自己写的程序,如果我要在子类里添加多一个新的属性“出生的年份”,并在main里执行。
person p1 = new Students(里面比上面多了个出生的年份)
应该怎么写? 展开
String name;
int age;
String professional;
Person(String name,int age,String professional) {
this.name= name;
this.age = age;
this.professional=professional;
}
public abstract void display();
}
class Students extends Person {
Students(String n,int a,String p) {
super(n,a,p);
}
public void display() {
System.out.println("我的姓名="+ name + " " + "我的年龄="+age +" " +"我的专业="+professional);
}
}
public class Test5 {
public static void main(String[] args) {
Person p = new Students("男",19,"美术");
Person o = new Students("女",14,"跳舞");
p.display();
o.display();
}
}
这个是我自己写的程序,如果我要在子类里添加多一个新的属性“出生的年份”,并在main里执行。
person p1 = new Students(里面比上面多了个出生的年份)
应该怎么写? 展开
展开全部
abstract class Person {
String name;
int age;
String professional;
Person(String name,int age,String professional) {
this.name= name;
this.age = age;
this.professional=professional;
}
public abstract void display();
}
class Students extends Person {
int year;
Students(String n,int a,String p,int year) {
super(n,a,p);
this.year=year;
}
public void display() {
System.out.println("我的姓名="+ name + " " + "我的年龄="+age +" " +"我的专业="+professional+"我的出生年为"+year);
}
}
public class Test5 {
public static void main(String[] args) {
Person p = new Students("男",19,"美术");
Person o = new Students("女",14,"跳舞");
p.display();
o.display();
}
}
String name;
int age;
String professional;
Person(String name,int age,String professional) {
this.name= name;
this.age = age;
this.professional=professional;
}
public abstract void display();
}
class Students extends Person {
int year;
Students(String n,int a,String p,int year) {
super(n,a,p);
this.year=year;
}
public void display() {
System.out.println("我的姓名="+ name + " " + "我的年龄="+age +" " +"我的专业="+professional+"我的出生年为"+year);
}
}
public class Test5 {
public static void main(String[] args) {
Person p = new Students("男",19,"美术");
Person o = new Students("女",14,"跳舞");
p.display();
o.display();
}
}
展开全部
你可在Students里添加出生年月属性date,然后增加一个构造方法
Students(String n,int a,String p,Date date) {
super(n,a,p);
date = this.date;
}
Students(String n,int a,String p,Date date) {
super(n,a,p);
date = this.date;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
子类可以有自己特有的属性,直接在子类里面写就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询