关于java调用子类问题

来源:百度知道 编辑:UC知道 时间:2024/05/18 11:05:20
public class Employee {
private String name;
private int id;
private int salary;

void method() {
name = "员工";
id = 0;
salary = 1000;
}//没有参数的构造方法,name默认为“员工”,id默认为0,salary默认为1000;

void method(String aname, int asalary) {
this.name = aname;
this.salary = asalary;
}//带参数aname和asalary的构造方法;

public void method(int aid, int asalary) {
this.method(name, 1000);
this.name = "Employee#";
System.out.println("name为" + name + "的id是:" + id);
}//有一个aid和asalary参数的构造方法,要求调用构造方法2,使name为“Employee#”连接id号;

public class Boss extends Employee{
//创建一个子类,继承Employee类;
private int bonus;
void Bmethod(){
super.method();
bonus=0;
}

void Bmethod(String aname

Boss是一个内部类,不能单独创建对象,必须通过它的外围类Employee类的对象来创建:

Boss boss2=new Boss();
改成
Employee e=new Employee();
Employee.Boss boss2= e.new Boss();]
这样就通过Employee的对象e创建了内部类Boss类的对象

另外,你使用了io包里面的类,方法,但是没有引入相应的包.

完整的程序如下:
import java.io.*;
public class Employee {
private String name;
private int id;
private int salary;

void method() {
name = "员工";
id = 0;
salary = 1000;
}//没有参数的构造方法,name默认为“员工”,id默认为0,salary默认为1000;

void method(String aname, int asalary) {
this.name = aname;
this.salary = asalary;
}//带参数aname和asalary的构造方法;

public void method(int aid, int asalary) {
this.method(name, 1000);
this.name = "Employee#";
System.out.println("name为" + name + "的id是:" + id);
}//有一个aid和asalary参数的构造方法,要求调用构造方法2,使name为“Employee#”连接id号;

public class Boss extends Employee{
//创