第6章 面向对象(高级篇)

   1、

interface ClassName {
    public String getClassName();
}
class Company implements ClassName {

    @Override
    public String getClassName() {
        return new Company().getClass().getName();
    }
    
}

public class CompanyTest {

    public static void main(String[] args) {
        ClassName cn = new Company();
        System.out.println(cn.getClassName());
    }

}

   2、

class Graph {
    private String color;//颜色
    private String shape;//图形
    
    public Graph(String color, String shape) {
        this.color = color;
        this.shape = shape;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getShape() {
        return shape;
    }

    public void setShape(String shape) {
        this.shape = shape;
    }
    
}

   3、

class Person {
    private String name;
    private String addr;
    private char sex;
    private int age;
    
    public Person(String name, String addr, char sex, int age) {
        this.name = name;
        this.addr = addr;
        this.sex = sex;
        this.age = age;
    }

    public Person(String name, char sex) {
        this.name = name;
        this.sex = sex;
    }
    
    public Person() {
        
    }
    
    public void show() {
        System.out.println("姓名:"+name+",年龄:"+age+",性别:"+sex+",地址:"+addr);
    }
    
}
class Student extends Person {
    private double math;
    private double english;
    
    public Student(String name, String addr, char sex, int age, double math, double english) {
        super(name, addr, sex, age);
        this.math = math;
        this.english = english;
    }

    public Student(double math, double english) {
        this.math = math;
        this.english = english;
    }
    
    public Student() {

    }
    
    public void show() {
        super.show();
        System.out.println("数学成绩:"+math+",英语成绩:"+english);
    }
    
}

   4、

abstract class Employee {
    private String name;
    private int age;
    private String sex;

    public Employee(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public abstract void show();
}

class Manager extends Employee {
    private String duty;
    private double salary;

    public Manager(String name, int age, String sex, String duty, double salary) {
        super(name, age, sex);
        this.duty = duty;
        this.salary = salary;
    }

    @Override
    public void show() {
        System.out.println("姓名:"+super.getName()+",年龄:"+super.getAge()+
                ",性别:"+super.getSex()+",职务:"+duty+",年薪:"+salary);
    }

}
class Pro extends Employee {
    private String department;
    private double monthSalary;
    public Pro(String name, int age, String sex, String department, double monthSalary) {
        super(name, age, sex);
        this.department = department;
        this.monthSalary = monthSalary;
    }
    @Override
    public void show() {
        System.out.println("姓名:"+super.getName()+",年龄:"+super.getAge()+
                ",性别:"+super.getSex()+",所属部门:"+department+",月薪:"+monthSalary);
    }
    

   5、

abstract class Shape {
    public abstract void area();
    public abstract void perimeter();
}
class Rectangle extends Shape {
    private double length;
    private double width;
    
    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public void area() {
        System.out.println("area="+length*width);
    }

    @Override
    public void perimeter() {
        System.out.println("perimeter="+(length+width)*2);
    }
    
}
class Circle extends Shape {
    public static final double PI = 3.14;
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public void area() {
        System.out.println("area="+PI * radius * radius);
    }

    @Override
    public void perimeter() {
        System.out.println("perimeter="+PI * radius * 2);
    }
    
}
class EquilateralTriangle extends Shape {
    private double length;

    public EquilateralTriangle(double length) {
        this.length = length;
    }

    @Override
    public void area() {
        System.out.println("area="+length*length*Math.sqrt(3) / 4);
    }

    @Override
    public void perimeter() {
        System.out.println("perimeter="+length*3);
    }
    
}

 

你可能感兴趣的:(第6章 面向对象(高级篇))