第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、形似P223_6.10 实例分析:宠物商店

/*
需求:使用面向对象的概念表示出下面的生活场景:
    小明去超市买东西,所有买到的东西都放在购物车中,最后到收银台一起结账
 */
abstract class Good {
    private String name;          //商品名称
    private double price;         //商品价格
    private int count;            //商品数量
    
    public Good(String name, double price, int count) {
        this.name = name;
        this.price = price;
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
    public abstract String getInfo();
    
}
class Book extends Good {
    private String author;         //作者
    private String publish;        //出版社
    
    public Book(String name, double price, int count, String author, String publish) {
        super(name, price, count);
        this.author = author;
        this.publish = publish;
    }

    @Override
    public String getInfo() {
        return "所购书名:"+super.getName()+",单价:"+super.getPrice()+
                ",购买册数:"+super.getCount()+",作者:"+author+",出版社:"+publish
                +",总计:"+super.getPrice()*super.getCount();
    }
}
class Cloth extends Good {
    private String brand;           //品牌
    private String style;           //款式
    
    public Cloth(String name, double price, int count, String brand, String style) {
        super(name, price, count);
        this.brand = brand;
        this.style = style;
    }

    @Override
    public String getInfo() {
        return "所购衣服名称:"+super.getName()+",单价:"+super.getPrice()+
                ",购买件数:"+super.getCount()+",衣服品牌:"+brand+",衣服样式:"+style
                +",总计:"+super.getPrice()*super.getCount();
    }
    
}
class ShopCar {
    private Good[] goods;
    private int foot = 0;

    public ShopCar(int len) {
        if(len > 0) 
            this.goods = new Good[len];
        else
            this.goods = new Good[1];
    }
    
    public boolean add(Good g) {
        boolean isAdd = false;
        if(foot != goods.length) {
            goods[foot] = g;
            foot++;
            isAdd = true;
        }
        return isAdd;
    }
    
    public Good[] search(String keyWord) {
        Good[] gs = null;
        int count = 0;
        for(int x = 0; x < goods.length; x++) {
            if(goods[x] != null) {
                if(goods[x].getName().indexOf(keyWord) != -1)
                    count++;
            }
        }
        gs = new Good[count];
        for(int i = 0, x = 0; i < goods.length; i++) {
            if(goods[i] != null) {
                if(goods[i].getName().indexOf(keyWord) != -1) {
                    gs[x] = goods[i];
                    x++;
                }
            }
        }
        return gs;
    }

    public Good[] getGoods() {
        return goods;
    }
    
}
public class Practice6 {

    public static void main(String[] args) {
        ShopCar sc = new ShopCar(5);
        sc.add(new Book("Java开发实战经典", 79.80, 1, "李兴华", "清华大学出版社"));
        sc.add(new Book("Thinking In Java", 89.80, 3, "Job", "机械工业出版社"));
        sc.add(new Book("西游记", 45.67, 56, "吴承恩", "商务印书馆"));
        sc.add(new Cloth("T恤", 100.50, 1, "李宁", "男士-L"));
        sc.add(new Cloth("牛仔裤", 356.78, 2, "森马", "男士-M"));
        System.out.println("===================已买到的商品清单===================");
        print(sc.getGoods());
        System.out.println("===================查询商品详细信息===================");
        try {
            print(sc.search(args[0]));
        } catch(Exception e) {
            System.out.println("未输入要查询商品或输入错误,"+"格式为:\njava Practice6  商品名(或商品名中的任意字符)");
        }
    }

    private static void print(Good[] goods) {
        if(goods != null)
            for(int x = 0; x < goods.length; x++) {
                System.out.println(goods[x].getInfo());
            }
    }

}

 

posted @ 2016-03-03 23:40  叶十一少  阅读(758)  评论(0编辑  收藏  举报