面向对象的应用(类的创建,封装,实例变量私有化,建立特定功能方法等)——以java语言为例

/*创建一个银行账户的类
思路:账户分为一个类;顾客信息分为一个类,其中顾客类中包含账户类信息;
//知识点:类对象的创建;实例变量私有化并进行封装;建立取款与存款功能方法。
*/
public class TotalAccount{
	public static void main(String[] args){

		Account account1= new Account("1000",2000.0);//创建一个名为account1的Account类对象
		Customer c1=new Customer("Jane Smith",account1);//创建一个名为c1的类对象
		System.out.println(account1.getBalance());
		System.out.println(account1.getId());
		System.out.println(c1.getName());
		account1.deposit(100.9);
		account1.withdraw(800);
		account1.withdraw(3000);
	}

}
class Customer{
	private String name;
	Account account;
	//建立新对象读取的方法
	public Customer(){}
	public Customer(String name,Account account){
		this.name=name;
		this.account=account;
	}
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name = name;
	}
	public Account getAccount(){
		return account;
	}
	public void setAccount(Account account){
		this.account = account;
	}
}
class Account{
	//实例变量私有化
	private String id;
	private double balance;
	//建立新对象读取的方法
	public Account(){}
	public Account(String id,double balance){
		this.id = id;
		this.balance=balance;
	}
	//建立get和set实例变量方法
	public String getId(){
		return id;
	}
	public void setId(String id){
		this.id = id;
	}
	public double getBalance(){
		return balance;
	}
	public void setBalance(double balance){
		this.balance = balance;
	}
	//建立取款方法
	public double withdraw(double money){
		if(money>balance){
			System.out.println("余额不足,取钱失败");
			return 0.0;
		}
		
		System.out.println("成功取出:"+money);
		System.out.println("剩余余额:"+(balance-money));

		return balance-money;
		

	}
	//建立存款方法
	public double deposit(double money){
		System.out.println("成功存入:"+money);
		System.out.println("剩余余额:"+(balance+money));
		return balance+money;
	}


}在这里插入代码片

效果图如下:
在这里插入图片描述

/* 封装  已知一个类student的代码如下:
class Student{
	String name;
	int age;
	String address;
	String zipCode;
	String mobile;
}
要求:1、把student的属性私有化,并提供get/set方法以及沟通的构建方法。
2、为student类添加一个getPostAddress方法,要求返回对象的地址和邮编。
*/
public class Student1{
	public static void main(String[] args){
		Student s1 =	new Student();
		
		System.out.println(s1.getPostAddress());
		s1.setAddress("北京");
		s1.setZipCode("200002");
		System.out.println(s1.getPostAddress());

	}
	
}





class Student{
	private String name;
	private int age;
	private String address;
	private String zipCode;
	private String mobile;
	public void student(){};
	public void student(String name,int age,String address,String zipCode,String mobile){
		this.name =name;
		this.age=age;
		this.address=address;
		this.zipCode=zipCode;
		this.mobile=mobile;
	}
	public String getPostAddress(){
		return "地址:"+this.getAddress()+",邮编:"+this.getZipCode();
	}


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

	public String getName(){
		return name;
	}
	public void setAge(int age){
		this.age=age;
	}
	public int getAge(){
		return age;
	}
	public void setAddress(String address){
		this.address=address;
	}
	public String getAddress(){
		return address;
	}
	public void setMobile(String mobile){
		this.mobile=mobile;
	}
	public String getMobile(){
		return mobile;
	}
	public void setZipCode(String zipCode){
		this.zipCode=zipCode;
	}
	public String getZipCode(){
		return zipCode;
	}
	

}在这里插入代码片

效果图如下:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值