java面向对象案例分析

编写一个代表地址的Address类
class Address {
	private String country;
	private String province;
	private String city;
	private String street;
	private String zipcode;

	public String getInfo() {
		return "country:" + this.country +
			   " province:" + this.province +
			   " city:" + this.city + 
			   " street: " + this.street +
			   " zipcode: " + this.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 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 {
	public static void main(String args[]) {
		System.out.println(new Address("中华人民共和国", "北京", "朝阳区", "天安门广场", "100001").getInfo());
	}
}
创建一个Employee类,员工属性包括编号、姓名、基本薪水、薪水增长率,还包括计算薪水增长额即计算机增长后的工资总额的操作方法。
class Employee {
	private long empno;
	private String ename;
	private double salary;
	private double rate;
	//setter getter 方法略

	public Employee() { }
	public Employee(long empno, String ename, double salary, double rate) {
		this.empno = empno;
		this.ename = ename;
		this.salary = salary;
		this.rate = rate;
	}
	//计算增长的薪水总额
	public double salaryIncValue() {
		this.salary = this.salary * this.rate;

		return this.salary;
	}
	//计算工资总额
	public double salaryIncResult() {
		return this.salary * (1 + this.rate);
	}
	public String getInfo() {
		return "工号:" + this.empno +
				" 姓名:" + this.ename +
				" 薪水:" + this.salary +
				" 增长率:" + this.rate;
	}
}

public class JavaDemo {
	public static void main(String args[]) {
		Employee emp = new Employee(1002L,"Love", 120000, 0.3);
		System.out.println(emp.getInfo());
		System.out.println("工资增长额度" + emp.salaryIncValue());
		System.out.println("增长后的工资:" + emp.salaryIncResult());
	}
}
构造一个银行账户类,类的构成如下内容:
  1. 数据成员用户的账户名称、用户的账户金额

  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;
	}

	//setter,getter
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public double getBalance() {
		return this.balance;
	}

	public String getInfo() {
		return "账户名称:" + this.name + " 金额:" + this.balance;
	}
}
public class JavaDemo {
	public static void main(String args[]) {
		Account acc = new Account("小仙女", 9000000);
		System.out.println(acc.getInfo());
		System.out.println(acc.getBalance());
	}
}
构造一个用户类
class User {
	private String uid;
	private String password;
	private static int count = 0;
	public User() {
		this("NOID", "fxs");
	}
	public User(String uid) {
		this(uid, "fxs");
	}
	public User(String uid, String password) {
		this.uid = uid;
		this.password = password;
		count ++;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword() {
		return this.password;
	}

	public static int getCount() {
		return count;
	}

	public String getInfo() {
		return "用户名:" + this.uid + 
				"\t密码:" + this.password;
	}
}

public class JavaDemo {
	public static void main(String args[]) {
		User userA = new User();
		User userB = new User("素素");
		User userC = new User("小仙女", "110");
		System.out.println(userA.getInfo());
		System.out.println(userB.getInfo());
		System.out.println(userC.getInfo());
		System.out.println(User.getCount());
	}
}

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值