抽象与集继承
1 package com.dengbao; 2 3 public class DemoPerson { 4 5 public static void main(String[] args) { 6 Person student=new Student("旺财",3, 90.5f); 7 Person worker=new Woker("小强",1, 8125.5f); 8 Person student1=new Student("小黄",8, 1000f); 9 Person worker1=new Woker("小刘",12, 18125.5f); 10 print(student.speak()); 11 print(worker.speak()); 12 print(student1.speak()); 13 print(worker1.speak()); 14 } 15 private static void print(String speak) { 16 System.out.println(speak); 17 } 18 }
1 package com.dengbao; 2 3 public abstract class Person { 4 private String name; 5 private int age; 6 public String getName() { 7 return name; 8 } 9 public void setName(String name) { 10 this.name = name; 11 } 12 public int getAge() { 13 return age; 14 } 15 public void setAge(int age) { 16 this.age = age; 17 } 18 public Person(String name, int age) { 19 this.name = name; 20 this.age = age; 21 } 22 public Person() { 23 super(); 24 } 25 @Override 26 public String toString() { 27 return "Person [name=" + name + ", age=" + age + "]"; 28 } 29 public abstract String speak(); 30 }
1 package com.dengbao; 2 3 public class Student extends Person{ 4 private float score; 5 6 public Student(String name, int age, float score) { 7 super(name, age); 8 this.setScore(score); 9 } 10 @Override 11 public String speak() { 12 return "学生说-->我的姓名:"+super.getName()+" "+super.getAge()+"岁 "+this.score+"分"; 13 } 14 public float getScore() { 15 return score; 16 } 17 public void setScore(float score) { 18 this.score = score; 19 } 20 }
This moment will nap, you will have a dream; but this moment study, you will interpret a dream.