学习Java – 解决初学者问题
请完成下面的程序。
(1) 类名:工资
① 字段:
String 名字
int 年薪② 创建者:
接受工资(String n, int s)并将名字和年薪赋值给字段
③ 方法:
通过getInfomation1()方法输出名字和年薪
(2) 类名:测试34 ==> 继承工资
① 字段:
String 部门
② 创建者:
继承名字、年薪和部门到字段,如果需要调用父类的创建者也可以
③ 方法:
通过getInfomation2()方法输出部门
(3) main() 函数以 Test34 ob = new Test34 (“野铁大将”、85000000, “铁器店”); 为起点
(4) 输出:名字:野铁大将
年薪:85000000
部门:铁器店
package firstStep;
public class Question34 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test34 ob = new Test34("野鉄大将", 85000000, "鉄器部屋");
ob.getInformation();
ob.getInformationV2();
}
}
class Salary {
String name;
int salary;
public Salary(String name, int salary) {
this.name = name;
this.salary = salary;
}
public void getInformation() {
System.out.printf("名前: %s", this.name);
System.out.printf("\n年俸: %s", this.salary);
}
}
class Test34 extends Salary {
String department;
public Test34(String name, int salary, String department) {
super(name, salary);
this.name = name;
this.salary = salary;
this.department = department;
}
public void getInformationV2() {
System.out.printf("\n部署: %s", this.department);
}
}