11.13 作业第十二次 工人、学生

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package sy;
 
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();
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package sy;
 
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;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package sy;
 
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;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package sy;
 
public class SandW {
 
 
    public static void main(String[] args) {
        Person student=new Student("旺财",23,90.5f);
        Person worker=new Worker("小强",45,18125.5f);
        print(student.speak());
        print(worker.speak());
    }
    private static void print(String speak) {
        System.out.println(speak);
    }
}

  

posted @   安静惠  阅读(144)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理
点击右上角即可分享
微信分享提示