Java学习路线-2:面向对象-类(4)

19、案例分析一(Address)

实现一个地址类,包含国家,省份,城市,街道,邮政编码

实现一个简单va" target="_blank">java类

class Address {
    private String country;
    private String province;
    private String city;
    private String street;
    private String zipcode;
    // setter
    public void setCountry(String country){
        this.country = country;
    }
    public void setProvince(String province){
        this.province = province;
    }
    public void setCity(String city){
        this.city = city;
    }
    public void setStreet(String street){
        this.street = street;
    }
    public void setZipcode(String zipcode){
        this.zipcode = zipcode;
    }
    // getter
    public String getCountry(){
        return this.country;
    }
    public String getProvince(){
        return this.province;
    }
    public String getCity(){
        return this.city;
    }
    public String getStreet(){
        return this.street;
    }
    public String getZipcode(){
        return this.zipcode;
    }
    // info
    public String getInfo(){
        return "国家: " + this.country +
               ", 省份: " + this.province +
               ", 城市: " + this.city +
               ", 街道: " + this.street +
               ", 邮政编码: " + this.zipcode;
    }
    public Address(String country, String province, String city, String street, String zipcode){
        this.country = country;
        this.province = province;
        this.city = city;
        this.street = street;
        this.zipcode = zipcode;
    }
    public static void main(String[] args) {
        Address address = new Address("中国", "北京", "朝阳", "大望路", "10001");
        System.out.println(address.getInfo());
        // 国家: 中国, 省份: 北京, 城市: 朝阳, 街道: 大望路, 邮政编码: 10001
    }
}

20、案例分析二(Employee)

实现一个员工类,包含编号,姓名,薪水,税率,还包括薪水增长计算和增长后的工资

class Employee{
    private long no;
    private String name;
    private double salary;
    private double rate;
    // setter getter ...
    public String getInfo(){
        return "编号:" + this.no +
                ", 姓名: " + this.name +
                ", 薪水 " + this.salary +
                ", 涨幅: " + this.rate;
    }
    public void increaseSalary(){
        this.salary = this.salary * (1 + this.rate);
    }
    public Employee(long no, String name, double salary, double rate){
        this.no = no;
        this.name = name;
        this.salary = salary;
        this.rate = rate;
    }
    public static void main(String[] args){
        Employee employee = new Employee(1L, "张三", 3000.0, 0.3);
        System.out.println(employee.getInfo());
        // 编号:1, 姓名: 张三, 薪水 3000.0, 涨幅: 0.3
        employee.increaseSalary();
        System.out.println(employee.getInfo());
        // 编号:1, 姓名: 张三, 薪水 3900.0, 涨幅: 0.3
    }
}

21、案例分析三(Dog)

创建Dog类,有名字,颜色,年龄,定义构造方法初始化属性

class Account{
    private String name;
    private double balance;
    public Account(String name){
        this(name, 0.0);
    }
    public Account(String name, double balance){
        this.name = name;;
        this.balance = balance;;
    }
    // 查询余额
    public double getBalance(){
        return this.balance;
    }
    public static void main(String[] args) {
        Account account = new Account("张三", 2000.0);
        System.out.println(account.getBalance());
        // 2000.0
    }
}

22、案例分析四(Account)

定义银行账户类,包括

1、数据:账户名称,账户余额

2、方法:开户(设置账号,余额),利用构造方法完成

3、查询余额

class Account{
    private String name;
    private double balance;
    public Account(String name){
        this(name, 0.0);
    }
    public Account(String name, double balance){
        this.name = name;;
        this.balance = balance;;
    }
    // 查询余额
    public double getBalance(){
        return this.balance;
    }
    public static void main(String[] args) {
        Account account = new Account("张三", 2000.0);
        System.out.println(account.getBalance());
        // 2000.0
    }
}

23、案例分析五(User)

创建用户类

1、用户名,记录用户个数

2、获取用户数

class User{
    private String name;
    private static int count = 0;
    public User(String name){
        this.name = name;
        count++;
    }
    public static int getCount(){
        return count;
    }
    public static void main(String[] args) {
        User user1 = new User("小明");
        User user2 = new User("小红");
        User user3 = new User("小花");
        System.out.println(User.getCount());
        // 3
    }
}

24、案例分析六(Book)

创建图书类

数据:书名,价格,编号(利用静态数据实现自动编号)

方法:统计总数

class Book{
    private int uid;
    private String name;
    private double price;
    private static int count = 0;
    public Book(String name, Double price){
        count++;
        this.uid = count;
        this.name = name ;
        this.price = price ;
    }
    public String getInfo(){
        return "<"+ this.uid + "> <<" + this.name +">> "+ this.price;
    }
    public static int getCount(){
        return count;
    }
    public static void main(String[] args) {
        Book book1 = new Book("今日头条", 12.0);
        Book book2 = new Book("百度", 14.0);
        Book book3 = new Book("腾讯", 11.0);
        System.out.println(Book.getCount());
        // 3
        System.out.println(book3.getInfo());
        // <3> <<腾讯>> 11.0
    }
}