码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA练习10

时间:2017-11-20 13:25:01      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:oat   nbsp   package   ring   work   sys   public   demo   main   

练习12

 
技术分享图片
package cn.ch.w;

public 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.name = name;
        this.age = age;
    }
    public Person() {
        super();
    }
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    public abstract String speak();
}
技术分享图片
技术分享图片
package cn.ch.w;
public class Student extends Person{
    private float score;
    
    public Student(String name, int age, float score) {
        super(name, age);
        this.setScore(score);
    }
    public String speak() {
        return "学生说-->我的姓名:"+super.getName()+" "+super.getAge()+"岁 "+this.score+"分";
    }
    public float getScore() {
        return score;
    }
    public void setScore(float score) {
        this.score = score;
    }
}
技术分享图片
技术分享图片
package cn.ch.w;

public class Worker extends Person {
    private float salary;
    
    public Worker(String name, int age, float salary) {
        super(name,age);
        this.setSalary(salary);
    }
    public String speak() {
        return "工人说 -->我的姓名:"+super.getName()+" "+super.getAge()+"岁 "+this.salary+"元";

    }
    public float getSalary() {
        return salary;
    }
    public void setSalary(float salary) {
        this.salary = salary;
    }
}
技术分享图片
技术分享图片
package cn.ch.w;

public class DemoStudentWorker {
    

        public static void main(String[] args) {
            Person student=new Student("他",3, 90.1f);
            Worker worker=new Worker("它",2, 25.5f);
            print(student.speak());
            print(worker.speak());
        }
        private static void print(String speak) {
            System.out.println(speak);
        }
    }
技术分享图片

 

 

JAVA练习10

标签:oat   nbsp   package   ring   work   sys   public   demo   main   

原文地址:http://www.cnblogs.com/qxc81518/p/7865193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!