Java作业求帮助 编写一个Java程序,在程序中定义一个PersonB类,定义一个P

2025-03-12 11:51:55
推荐回答(2个)
回答1:

class PersonB
{
String name ;
int age;
public PersonB()
{
System.out.println("PersonB()被调用");
}
public PersonB(String newName)
{
name = newName;
System.out.println("PersonB(String newName)被调用");
}
public void introduce( )
{
System.out.println("我是"+name+",今年"+age+"岁");
}
}
class StudentB extends PersonB
{
// 【代码1】 //创建一个参数为空的StudentB类构造方法,能显示“StudentB() 被调用”
public StudentB(){
System.out.println("StudentB() 被调用");
}
public StudentB(String newName,int newAge)
{
// 【代码2】 //调用父类的public PersonB(String newName)类构造方法,传入newName参数,提示使用关键词super进行调用
super(newName);
// 【代码3】 //将newAge赋值给age属性
super.age = newAge;
}
}
class C2
{
public static void main(String []args)
{
StudentB s1 = new StudentB();
StudentB s2 = new StudentB("张三",19);
// 【代码4】 //调用s2的 introduce方法
s2.introduce();
}
}

纯手打,采纳采纳!!!!!!!!11

回答2:

class StudentB extends PersonB
{
//创建一个参数为空的StudentB类构造方法,能显示“StudentB()被调用”
public StudenB(){
System.out.println("StudentB()被调用");

}
public StudentB(String newName,int newAge)
{
//调用父类的public PersonB(String newName)类构造方法,传
//入newName参数,提示使用关键词super进行调用
super(newName);
//将newAge赋值给age属性
age=newAge;
}
}
class C2
{
public static void main(String []args)
{
StudentB s1 = new StudentB();
StudentB s2 = new StudentB(“张三”,19);
//调用s2的 introduce方法
s2.introduce( );
}
}