当前位置: 首页 > news >正文

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

一,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);
    }

}

相关文章:

  • SQLite SQL调优指南及高级SQL技巧
  • 19 基于51单片机的倒计时音乐播放系统设计
  • 举例说明 .Net Core 单元测试中 xUnit 的 [Theory] 属性的用法
  • 【iOS】计算器仿写
  • 数据链路层
  • MongoDB-aggregate流式计算:带条件的关联查询使用案例分析
  • MKV转MP4丨FFmpeg的简单命令使用——视频格式转换
  • 小蒋聊技术——DevOps 是什么“玩意”?
  • 爬虫——爬虫理论+request模块
  • SpringBoot Jar 包加密防止反编译
  • Django学习笔记一:MVT的示例
  • 用java编写飞机大战
  • OpenAPI3常用注解
  • pygame--超级马里奥(万字详细版)
  • 【Android 源码分析】Activity生命周期之onDestroy
  • 游戏中的对象池技术探索(一)
  • C语言之文件操作
  • GWAS分析中显著位点如何注释基因:excel???
  • 利用Python进行文本处理的9个实用函数
  • 华为仓颉语言入门(9):for-in表达式
  • 中国目的地·入境游简报006|外国网红游中国启示录
  • 演员发文抵制代拍获粉丝支持,媒体:追星“正确姿势”不妨多来点
  • 中山大学人类学系原系主任冯家骏逝世,享年95岁
  • 肖峰读《从塞北到西域》︱拉铁摩尔的骆驼
  • 国家主席习近平在莫斯科出席红场阅兵式
  • 河南省平顶山市副市长许红兵主动投案,接受审查调查