zoukankan      html  css  js  c++  java
  • Java第十二次作业:继承与抽象类解决工人与学生的问题,抽象类实例。抽象类作用——为多态创造了可能。抽象类的作用总结

    继承与抽象类解决工人与学生的问题


    抽象类实例

    package com.swift;
    
    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();
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
        public abstract String speak();
    }
    package com.swift;
    
    public class Student extends Person{
        private float score;
        
        public Student(String name, int age, float score) {
            super(name, age);
            this.setScore(score);
        }
        @Override
        public String speak() {
            return "学生说-->我的姓名:"+super.getName()+" "+super.getAge()+"岁 "+this.score+"分";
        }
        public float getScore() {
            return score;
        }
        public void setScore(float score) {
            this.score = score;
        }
    }
    package com.swift;
    
    public class Worker extends Person {
        private float salary;
        
        public Worker(String name, int age, float salary) {
            super(name, age);
            this.setSalary(salary);
        }
        @Override 
        public String speak() {
            return "工人说 -->我的姓名:"+super.getName()+" "+super.getAge()+"岁 "+this.salary+"元";
        }
        public float getSalary() {
            return salary;
        }
        public void setSalary(float salary) {
            this.salary = salary;
        }
    }
    package com.swift;
    
    public class DemoStudentWorker {
    
        public static void main(String[] args) {
            Person student=new Student("旺财",3, 90.5f);
            Person worker=new Worker("小强",1, 8125.5f);
            print(student.speak());
            print(worker.speak());
        }
        private static void print(String speak) {
            System.out.println(speak);
        }
    }

    抽象类的作用——为多态创造了可能


    抽象类的作用总结


  • 相关阅读:
    文科妹子都会用 GitHub,你这个工科生还等什么
    阿里巴巴开发手册强制使用SLF4J作为门面担当的秘密,我搞清楚了
    天啦撸!打印日志竟然只晓得 Log4j?
    老板下了死命令,要把日志系统切换到Logback
    根号x的导数,求导方法
    Java内存模型
    loadrunner截取变量的字符串
    loadrunner11回放日志中文乱码解决办法
    软件性能测试的几个主要术语
    什么是自动化测试框架
  • 原文地址:https://www.cnblogs.com/qingyundian/p/7737069.html
Copyright © 2011-2022 走看看