当前位置: 代码迷 >> 综合 >> 面向对象案例分析 - Java基础知识 3
  详细解决方案

面向对象案例分析 - Java基础知识 3

热度:111   发布时间:2023-10-23 03:14:01.0

学习笔记

初期最可靠也是最简单的分析依据:简单Java类;

案例一

编写并测试一个代表地址的address类,地址由国家、省份、城市、街道、邮编组成,并返回完整信息。

class Address{private String country ;private String province ;private String city ;private String street ;private String zipcode ;// 构造方法public Address(){}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  ;}// stter getterpublic String getInfo(){return "国家:" + this.country + "、省份:" + this.province + "、城市:" + this.city +"、街道:" + this.street +"、邮编:" + this.zipcode ;}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 ;}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 ;}}public class JavaDemo{ // 主类public static void main(String arges[]){Address address = new Address("中国", "四川", "成都", "电子科大", "000001"); System.out.println(address.getInfo()) ;}}

案例二

定义并测试一个代表员工的Employee类。员工属性包括“编号”、“姓名”、“基本工资”、“薪水增长率”,包括计算薪水增长额以及计算增长后的工资总额的操作方法。

这个程序的功能已经超过简单Java类的范畴,因为简单Java类中不包括复杂的逻辑,但是思考依然从简单Java类开始。

class Employee{private long empno ;private String ename      ;private double salary;private double rate ;public Employee(){}public Employee(long empno, String ename, double salary, double rate){this.empno = empno ;this.ename = ename ;this.salary = salary ;this.rate  = rate  ;}// setter getter略public String getInfo(){return "编号:" + empno + "、姓名:" + ename + "、工资:" + salary +"、增长率:" + rate ;}public double salaryIncValue(){ //的到薪水增长额度return this.salary * this.rate ;}public double salaryIncResult(){ // 薪水总额this.salary = this.salary * (1 + this.rate) ;return this.salary ;}}public class JavaDemo{ // 主类public static void main(String arges[]){Employee employee = new Employee(001,"张三", 12000.00, 0.01) ;System.out.println(employee.getInfo()) ;System.out.println(employee.salaryIncValue()) ;System.out.println(employee.salaryIncResult()) ;System.out.println(employee.getInfo()) ;}}

案例三

构造一个银行账户类,类的构成包含如下如下:

  1. 数据成员用户的账户名称、用户账户余额(private数据类型)。
  2. 方法包括开户(设置账户名称及余额),利用构造方法完成。
  3. 查询余额。
class Acount{private String ename ;private double balance ;public Acount(){}public Acount(String ename){this(ename, 0.0) ;}public Acount(String ename, double balance){this.ename = ename ;this.balance = balance ;}//setter getter略public String checkBalance(){return "账户姓名:" + this.ename +"、账户余额:" + this.balance ;}
}public class JavaDemo{ // 主类public static void main(String arges[]){Acount acount = new Acount("张三") ;System.out.println(acount.checkBalance()) ;}}

案例四

设计一个表示用户的user类,类中有用户名、口令、记录用户个数的变量,定义类的三个构造,获取和设置口令的方法和返回类信息。

方法一:使用静态代码块 

class User{private String uid ;private long password ;private static int count = 1 ;static{count ++ ;}public User(){}public User(String uid){this(uid, 0);}public User(String uid, long password){this.uid = uid ;this.password = password ;}// setter getter 略public static int getCount(){return count ;}public String getInfo(){return "用户名:" + this.uid + "、口令:" + this.password +"、已有用户数目:" + count ;}public void setComand(long password){this.password = password ;}
}public class JavaDemo{ // 主类public static void main(String arges[]){User user1 = new User() ;User user2 = new User() ;System.out.println(user2.getInfo() ) ;}}

方法二

class User{private String uid ;private String password ;private static int count = 0 ;public User(){this("无名氏", "0000") ;}public User(String uid){this(uid, "00000") ;}public User(String uid, String password){this.uid = uid ;this.password = password ;count ++ ;}// setter getter 略public static int getCount(){return count ;}public String getInfo(){return "用户名:" + this.uid + "、口令:" + this.password +"、已有用户数目:" + count ;}public void setComand(String password){this.password = password ;}
}public class JavaDemo{ // 主类public static void main(String arges[]){User user1 = new User() ;User user2 = new User() ;System.out.println(User.getCount() ) ;}}

案例五

声明一个图书类,其数据成员为书名、编号(使用静态变量自动编号)、书价,并拥有静态数据成员册数、图书总册数,在构造方法中利用次静态变量为对象赋值,在主方法中定义多个对象,并求出总册数。

class Book{private int bid ; // 编号private String title ; //书名private double price ; // 价格private static int  count = 0 ;public Book(){this("未知", 0.0) ;}public Book(String title){this(title, 0.0) ;}public Book(String title, double price){this.bid = count ;this.title = title ;this.price = price ;count ++ ;}// setter getter 略public String getInfo(){return "书名:" + this.title +"、价格:" + this.price + "、编号:" + this.bid ;}public static int getCount(){return count ;}
}public class JavaDemo{ // 主类public static void main(String arges[]){Book book1 = new Book();Book book2 = new Book("java",100.00) ;Book book3 = new Book("JSP") ;System.out.println(book1.getInfo()) ;System.out.println(book2.getInfo()) ;System.out.println(book3.getInfo()) ;System.out.println("图书总册数:" + Book.getCount()) ;}}

在面向对象的基础开发中,简单Java类是解决先期设计的最好解决方案。