abstract class Person{
private String name;
private int age ;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

public Person(String name , int age){
this.setName(name);
this.setAge(age);
}

public abstract String getSay();

public void say(){
System.out.println(this.getSay());
}
}class Worker extends Person{
private float money ; public float getMoney() {
return money;
} public void setMoney(float money) {
this.money = money;
}

public Worker(String name,int age, float money){
super(name,age);
this.setMoney(money);
}

public  String getSay(){
return "我是一个工人,名字叫"+this.getName()+"年龄是"+this.getAge()+" , 工资"+this.getMoney();
}
}class Student extends Person{
private float score ; public float getScore() {
return score;
} public void setScore(float score) {
this.score = score;
}

public Student(String name , int age , float score){
super(name,age);
this.setScore(score);
}

public String getSay(){
return "我是一名学生,名字叫"+super.getName()+",年龄"+super.getAge()+" , 成绩"+this.getScore();
}
}public class Demo1 { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Worker("张三",12,34);
p.getSay();
}
}为什么这段代码执行起来 什么输出都没有呢?

解决方案 »

  1.   

    p.getSay();只是返回一个字符串啊,打印的语句呢?要调用 say()才有东西
      

  2.   

    public static void main(String[] args) {
            // TODO Auto-generated method stub
            Person p = new Worker("张三",12,34);
            p.say();
        }
      

  3.   

    abstract class Person{
        private String name;
        private int age ;
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
        public Person(String name , int age){
            this.setName(name);
            this.setAge(age);
        }
        
        public abstract String getSay();
        
        public void say(){
            System.out.println(this.getSay());
        }
    }class Worker extends Person{
        private float money ;    public float getMoney() {
            return money;
        }    public void setMoney(float money) {
            this.money = money;
        }
        
        public Worker(String name,int age, float money){
            super(name,age);
            this.setMoney(money);
        }
        
        public  String getSay(){
            return "我是一个工人,名字叫"+this.getName()+"年龄是"+this.getAge()+" , 工资"+this.getMoney();
        }
    }class Student extends Person{
        private float score ;    public float getScore() {
            return score;
        }    public void setScore(float score) {
            this.score = score;
        }
        
        public Student(String name , int age , float score){
            super(name,age);
            this.setScore(score);
        }
        
        public String getSay(){
            return "我是一名学生,名字叫"+super.getName()+",年龄"+super.getAge()+" , 成绩"+this.getScore();
        }
    }public class Demo1 {    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Person p = new Worker("张三",12,34);
            p.say();
        }
    }运行结果:我是一个工人,名字叫张三年龄是12 , 工资34.0