- abstract class Employee {
- private String name; //方案1 将抽象类中的私有变量公有 ,不建议
- private String id; //public String id......;
- private double pay;
- Employee(String name, String id, double pay) {
- this.name = name;
- this.id = id;
- this.pay = pay;
- }
- String getName(){
- return name;
- }
- // 方案二 第一步定义一个方法,可以让子类或外部对象对过此方法拿到本类中的私有变量,
- //建议此方法
- String getId(){
- return id;
- }
- double getPay(){
- return pay;
- }
-
-
- public abstract void work();
- }
- class Manager extends Employee {
- private int bouns;
- Manager(String name, String id, double pay, int bouns) {
- super(name, id, pay);
- this.bouns = bouns;
- }
- public void work() {
- System.out.println("name=" +getName()+" " + "id=" + getId()+" " + "pay=" +getPay()+" "
- + "bouns="+bouns);
- //方案二第二步,在此调用get方法
- }
- }
- class Pro extends Employee {
- Pro(String name, String id, double pay) {
- super(name, id, pay);
- }
- public void work() {
- System.out.println("employee work");
- }
- }
- public class AllEmployee {
- public static void main(String args[])
- {
- Manager m = new Manager("zhangsan", "2012", 2010, 120);
- m.work();
- }
- }
复制代码 /*
如上代码怎么在System.out.println("name="+" "+"id="+" "+"pay="+" "+"bouns=");这一行中打印相应的成员变量
方案1 将抽象类中的私有变量公有 ,不建议方案
方案2 定义一个getxx方法,可以让子类或外部对象对过此get方法拿到本类中的私有变量,
*/
|