初学Java基础Day14---初识面向对象,private关键字和封装及构造方法习题

发布于:2024-10-09 ⋅ 阅读:(158) ⋅ 点赞:(0)

一,private-私有化

理解:private是访问修饰符的一种,访问修饰符规定了访问权限。

作用:1.修饰属性:该属性只能在类的内部使用;

2.修饰方法,该方法只能在类的内部使用;

// private 修饰方法使用

public class Test01{
    public static void main(String[] args){
        A a = new A();
        a.method02();
    }
}

public class A{

    String str = "周杰伦";
    
    private void method01(){
        System.out.println(str)      
    }
    public void method02(){

        method01();
    }
}

二,封装

1.需求:

模拟银行用户操作余额的功能

2.分析

操作余额(存钱,取钱,查看)

3.代码实现
public class Test01{
    public static void main(String[] args){
        User user = new User("2289751581", "123123", "周杰伦", "123456789", '男', 2000);
        
        //存钱
      user.setSurplus(user.getSurplus() + 200);//先看括号里面的,先获取原来的钱2000再加200,然后设置
        
        //取钱
        user.setSurplus(user.getSurplus()-1800);//括号里面先获取原来的钱2200再减1800,然后设置

        //查看
        System.out.println(user.getSurplus());//获取
    }
}
public class User{
    String userName;
    String password;
    String name;
    String phone;
    char sex;
    private double surplus;//余额
    
    public User() {
	}

	public User(String username, String password, String name, String phone, char sex, double surplus) {
		this.username = username;
		this.password = password;
		this.name = name;
		this.phone = phone;
		this.sex = sex;
		this.surplus = surplus;
	}
    
    //设置余额
    public void setSurplus(double surplus){
        
        //额外的功能
		double num = (surplus - this.surplus);//现在金额-原来金额
		System.out.println(LocalDateTime.now() + " -- " + this.name + "用户操作了金额:" + ((num>0)?"+":"") + num);

        	//设置属性
			this.surplus = surplus;
    }
    
    //获取余额
    public double getSurplus(){
        
        //额外的功能
		System.out.println(LocalDateTime.now() + " -- " + this.name + "用户获取了金额");

        //返回余额
        return surplus;
    }

}

理解:不能直接操作属性,可以添加get/set方法;

步骤:1.私有化属性 2. 添加 get(获取), set(设置)方法

好处:外界不能直接操作属性(有风险),通过get/set方法操作属性,可以在方法 内添加额外功能。

封装:把一个对象的属性私有化,同时提供一些可以被外界访问属性的方法

三,习题:小明把大象关进冰箱

1、分析

大象类:---属性:品种,姓名,性别   方法:吃,叫

冰箱类:--属性:品牌,价格        方法:打开冰箱,关闭冰箱,装大象
  人类:---属性:姓名,性别,年龄   方法:打开冰箱,关闭冰箱,装大象

2.代码实现
public class Test01{
    public static void main(String[] args){
        Person p = new Person("小明",'男',18);
	        Elephant e = new Elephant("小飞侠",'公',"亚洲象");
	        Fridge f = new Fridge("小米",3999);
	        
	        p.openFridge(f);
	        p.loadElephant(e,f);
	        p.closeFridge(f);
        
    }
}

//大象类
public class Elephant{
    String name;
    char sex;
    String type;
    
    public Elephant(){
        
    }
    
    public Elephant(String name,char sex,String type){
       this.name = name;
		this.sex = sex;
		this.type = type;

    }
    
    public void eat(){
        System.out.println(this.name+"吃香蕉");
    }
    public void shout(){
        System.out.println(this.name+"嗷嗷嗷~~~");
    }
}

//冰箱类
public class Fridge{
    String brand;
    double price;
    
    public Fridge(){
        
    }
    public Fridge(String brand,double price){
        this.brand = brand;
		this.price = price;

    }
    
    public void open(){
        System.out.println(this.brand+"冰箱开门");
    }
    public void close(){
        System.out.println(this.brand+"冰箱关门");
    }
    
    public void load(Elephant e){
        e.eat();
        e.shout();
        System.out.println(this.brand+"牌冰箱装"+e.name);
    }
}

//人类

public class Person{

    String name;
	char sex;
	int age;

    public Person() {
	}
	
	public Person(String name, char sex, int age) {
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
    
     public void openFridge(Fridge f){
        f.open();
    }
    public void closeFridge(Fridge f){
		f.close();
    }
    public  void loadElephant(Elephant e,Fridge f){
        f.load(e);
    }

}


网站公告

今日签到

点亮在社区的每一天
去签到