JAVA exercise 10 and java exercise

Source: Internet
Author: User

JAVA exercise 10 and java exercise

Exercise 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 "student says --> my name:" + super. getName () + "" + super. getAge () + "years old" + this. score + "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 "said --> my name:" + super. getName () + "" + super. getAge () + "years old" + 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 ("he", 3, 90.1f ); worker worker = new Worker ("it", 2, 25.5f); print (student. speak (); print (worker. speak ();} private static void print (String speak) {System. out. println (speak );}}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.