文章目录
1 案例分析一(Address)
编写测试一个代表地址的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 ;
}
public String getInfo(){
return "国家: " + this.country +
"省份: " + this.province +
"城市: " + this.city +
"街道: " + this.street +
"邮编: " + this.zipcode;
}
public void setCountry(String country){
this.country = country ;
}
public String getCountry(){
return this.country ;
}
public void setProvince(String Province){
this.province = province ;
}
public String getProvince(){
return this.province ;
}
public void setCity(String city){
this.city = city ;
}
public String getCity(){
return this.city ;
}
public void setStreet(String street){
this.street = street ;
}
public String getStreet(){
return this.street;
}
public void setZipcode(String zipcode){
this.zipcode = zipcode;
}
public String getZipcode(){
return this.zipcode;
}
}
public class JavaDemo {
{
System.out.println("***构造代码块1执行!***") ;
}
static {
System.out.println("***静态代码块1执行!***") ;
}
public static void main(String[] args){
{//普通代码块
int x = 10; //局部变量
System.out.println(x);
}
int x = 100; //全局变量
System.out.println(x);
System.out.println(new Address("1", "2", "3", "4", "5").getInfo());
}
}
2 案例分析二(Employee)
定义并测试一个代表员工的Employee类。员工属性包括“编号”、“姓名”、“基本薪水“、”薪水增长率“,还包括计算薪水增额及计算增长后的工资总额的操作方法。
class Employee{
private String code;
private String name;
private double salary;
private double increaserate;
public Employee(){}
public Employee(String code, String name, double salary, double increaserate){
this.code = code;
this.name = name;
this.salary = salary;
this.increaserate = increaserate;
}
public String getInfo(){
return "Code: " + this.code +
"; Name: " + this.name +
"; Salary: " + this.salary +
"; IncreaseRate: " + this.increaserate;
}
public double getIncreaseMoney(){
return this.salary * this.increaserate;
}
public double getIncreasedSalary(){
return this.salary + this.getIncreaseMoney();
}
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 setIncrease(double increaserate){
this.increaserate = increaserate;
}
public String getCode() {
return this.code;
}
public String getName() {
return this.name;
}
public double getSalary(){
return this.salary;
}
public double getIncreaseRate(){
return this.increaserate;
}
}
public class JavaDemo{
public static void main(String[] args){
Employee employee1 = new Employee("00001", "lks", 3000, 0.1);
System.out.println(employee1.getInfo());
System.out.println(employee1.getIncreaseMoney());
System.out.println(employee1.getIncreasedSalary());
}
}
3 案例分析三(Dog)
设计一个Dog类,有名字、颜色、年龄等属性,定义构造方法来初始化类的这些属性,定义方法输出Dog信息,编写应用程序使用Dog类。
class Dog{
private String name;
private String color;
private int age;
public Dog(){}
public Dog(String name, String color, int age){
this.name = name;
this.color = color;
this.age = age;
}
public String getInfo(){
return "Name: " + this.name +
"; Color: " + this.color +
"; Age: " + this.age;
}
public void setName(String name){
this.name = name;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return this.name;
}
public String getColor(){
return this.color;
}
public int getAge(){
return this.age;
}
}
public class JavaDemo{
public static void main(String[] args){
Dog dog = new Dog("kiki", "blue", 6);
System.out.println(dog.getInfo());
}
}
4 案例分析四(Account)
构造一个银行账户类,类的构成包括如下内容:
(1)数据成员用户的账户名称、用户的账户余额(private数据类型)。
(2)方法包括开户(设置账户名及余额),利用构造方法完成。
(3)查询余额。
class Account{
private String name;
private double balance;
public Account(){}
public Account(String name){
this(name, 0.0);
}
public Account(String name, double balance){
this.name = name;
this.balance = balance;
}
public String getInfo(){
return "Name: " + this.name +
"; Balance: " + this.balance;
}
public double getBalance(){
return this.balance;
}
public void setName(String name){
this.name = name;
}
public void setBalance(double balance){
this.balance = balance;
}
public String getName(){
return this.name;
}
}
public class JavaDemo{
public static void main(String[] args){
Account account = new Account("lks", 90000.0);
System.out.println(account.getInfo());
System.out.println(account.getBalance());
}
}
5 案例分析五(User)
设计一个表示用户的User类,类中的变量有用户名、口令和记录用户个数的变量,定义类的三个构造方法(无参、为用户名赋值、为用户名和口令赋值)、获取和设置口令的方法和返回类信息的方法。
class User{
private String uid;
private String password;
private static int count = 0;
public User(){
this("NOID", "lks");
}
public User(String uid) {
this(uid, "hhy");
}
public User(String uid, String password){
this.uid = uid;
this.password = password;
User.count++;
}
public String getInfo(){
return "Uid: " + this.uid +
"; Password: " + this.password;
}
public static int getCount(){
return User.count;
}
public void setUid(String uid){
this.uid = uid;
}
public void setPassword(String password){
this.password = password;
}
public String getUid(){
return this.uid;
}
public String getPassword(){
return this.password;
}
}
public class JavaDemo{
public static void main(String[] args){
User userA = new User();
User userB = new User("lks");
User userC = new User("hhy", "iloveyou");
System.out.println(userA.getInfo());
System.out.println(userB.getInfo());
System.out.println(userC.getInfo());
System.out.println(User.getCount());
}
}
6 案例分析六(Book)
声明一个图书类,其数据成员为书名、编号(利用静态变量实现自动编号)、书价,并拥有静态数据成员册数、记录图书的总册数,在构造方法中利用此静态变量为对象编号赋值,在主方法中定义多个对象,并求出总册数。
class Book{
private String name;
private int code;
private double price;
private static int total = 0;
public Book(){}
public Book(String name, double price){
this.name = name;
this.price = price;
this.code = ++Book.total;
}
public String getInfo(){
return "Name: " + this.name +
" ;Code: " + this.code +
" ;Price: " + this.price;
}
public static int getTotal(){
return Book.total;
}
}
public class JavaDemo{
public static void main(String[] args){
Book bookA = new Book("lks", 12.2);
Book bookB = new Book("hhy", 23);
Book bookC = new Book("love", 52);
System.out.println(bookA.getInfo());
System.out.println(bookB.getInfo());
System.out.println(bookC.getInfo());
System.out.println(Book.getTotal());
}
}
7 总结
在面向对象最基础的开发里面,简单Java类是解决先期设计最好的方案。