面向对象案例分析

发布于2020-08-17   612 次阅读


本文最后更新于2022年6月1日,已超过 1 年没更新!内容可能已失效,请自行测试。

案例分析一(Address)

/** 编写并测试一个代表地址的Adress类,地址信息由国家、省份、城市、街道、邮编组成,并可以返回玩这个的地址信息 */
class Address {
  private String country;
  private String state;
  private String city;
  private String street;
  private int postcode;

  public Address() {
    this("中国", "四川", "成都", "牛鼻路", 650000);
  }

  public Address(String country) {
    this(country, "四川", "成都", "牛鼻路", 650000);
  }

  public Address(String country, String state) {
    this(country, state, "成都", "牛鼻路", 650000);
  }

  public Address(String country, String state, String city) {
    this(country, state, city, "牛鼻路", 650000);
  }

  public Address(String country, String state, String city, String street) {
    this(country, state, city, street, 650000);
  }

  public Address(String country, String state, String city, String street, int postcode) {
    this.country = country;
    this.state = state;
    this.city = city;
    this.street = street;
    this.postcode = postcode;
  }

  public void setCountry() {
    this.country = country;
  }

  public void setState(String state) {
    this.state = state;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public void setStreet(String street) {
    this.street = street;
  }

  public void setPostcode(int postcode) {
    this.postcode = postcode;
  }

  public String getCountry() {
    return country;
  }

  public String getState() {
    return state;
  }

  public String getCity() {
    return city;
  }

  public String getStreet() {
    return street;
  }

  public int getPostcode() {
    return postcode;
  }

  public String getInfo() {
    return country + " " + state + " " + city + " " + street + " " + postcode;
  }
}

public class AddressDemo {
  public static void main(String[] args) {
    Address address = new Address();
    System.out.println(address.getInfo());
  }
}

案例分析二(Employee)

/** 编写并测试一个代表员工的Empolyee类 员工属性包括"编号","姓名","基本工资","薪水增长率” 包括计算薪水增长额 以及计算增长后的工资总额的方案 */
class Employee {
  private String code;
  private String name;
  private double salary;
  private double grow;

  public Employee() {
    this("1", "张三", 5000, 0.2);
  }

  public Employee(String code, String name, double salary, double grow) {
    this.code = code;
    this.name = name;
    this.salary = salary;
    this.grow = grow;
  }

  public void setCode(String code) {
    this.code = code;
  }

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

  public void setSalary(double salary) {
    this.salary = salary;
  }

  public void setGrow(double grow) {
    this.grow = grow;
  }

  public String getName() {
    return name;
  }

  public double getGrow() {
    return grow;
  }

  public double getSalary() {
    return salary;
  }

  public String getCode() {
    return code;
  }

  public double cacSalaryGrow() {
    return this.salary * this.grow;
  }

  public double cacSalary() {
    return this.salary * (1 + this.grow);
  }
}

public class EmployeeDemo {
  public static void main(String[] args) {
    Employee stu = new Employee();
    System.out.println(stu.cacSalary());
  }
}

案例分析三(Dog)

/** 设计一个Dog类,有名字,颜色,年龄等属性 定义构造方法来初始化类的这些属性 ,定义方法输出Dog信息 */
class Dog {
  private String name;
  private String color;
  private int age;

  public Dog(String name, String color, int age) {
    this.name = name;
    this.color = color;
    this.age = age;
  }

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

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

  public void setAge(int age) {
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public String getColor() {
    return color;
  }

  public int getAge() {
    return age;
  }

  public String getInfo() {
    return name + color + age;
  }
}

public class DogDemo {
  public static void main(String[] args) {
    Dog dog = new Dog("啤酒", "黄色", 2);
    System.out.println(dog.getInfo());
  }
}

案例分析四(Account)

/** 构造一个银行账户类,类的构成包括如下内容 数据成员用户的账户名称,用户的账户余额 方法包括开户 *查询余额 */
class Account {
  private String name;
  private String money;

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

  public void setMoney(String money) {
    this.money = money;
  }

  public String getName() {
    return name;
  }

  public String getMoney() {
    return money;
  }

  public Account(String name, String money) {
    this.name = name;
    this.money = money;
  }

  public Account() {}

  public String trash() {
    return this.money;
  }
}

public class AccountDemo {
  public static void main(String[] args) {
    Account ac = new Account("张三", "20000");
    System.out.println(ac.trash());
  }
}

案例分析五(User)

/** 设计一个表示用户的User类,类中的变量有用户名,口令和记录用户个数的变量,定义类的三个构造方法,获取和设置口令的方法和返回类信息的方法 */
class User {
  private String name;
  private String passwd;
  private static int count = 0;

  public User() {
    this("NOID", "0");
  }

  public User(String name) {
    this(name, "123");
  }

  public User(String name, String passwd) {
    this.name = name;
    this.passwd = passwd;
    count++;
  }

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

  public void setPasswd(String passwd) {
    this.passwd = passwd;
  }

  public String getName() {
    return name;
  }

  public String getPasswd() {
    return passwd;
  }

  public static int getCount() {
    return count;
  }

  public String getInfo() {
    return "用户名" + this.name + "密码" + this.passwd;
  }
}

public class UserDemo {
  public static void main(String[] args) {
    User userA = new User();
    User userB = new User("小强");
    User userC = new User("大强", "666");
    System.out.println(userA.getInfo());
    System.out.println(userB.getInfo());
    System.out.println(userC.getInfo());
    System.out.println(User.getCount());
  }
}

案例分析六(Book)

// 声明一个图书类,其数据成员为书名,编号,书价,并拥有 静态数据成员册数,记录图书的册数
// 在构造方法中利用此静态变量为对象的编号赋值,在主方法中定义多个对象,求出总册数
class book {
  private int uid;
  private String title;
  private double price;
  private static int count = 0;

  public book(String title, double price) {
    this.uid = count + 1;
    this.title = title;
    this.price = price;
    count++;
  }

  public String getINFO() {
    return "编号" + this.uid + "书名" + this.title + "价格" + this.price;
  }

  public static int getCount() {
    return count;
  }
}

public class BookDemo {
  public static void main(String[] args) {
    book book1 = new book("Java", 19.9);
    book book2 = new book("Jsp", 9.9);
    book book3 = new book("Web", 29.9);
    System.out.println(book1.getINFO());
    System.out.println(book2.getINFO());
    System.out.println(book3.getINFO());
    System.out.println(book.getCount());
  }
}

=

一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。