Java开发必备点07_引用之间传递 this 关键字 封装

引用之间传递 this 关键字 封装

引用之间的传递

简单数据类型之间传递的是值
引用数据类型之间传递的是地址
案例:
public class Demo{
    public static void main(String[] args) {
        /*int a = 10;
        int b = a;
        b++;
        System.out.println(a);//10*/

        //创建对象
        Student s1 = new Student();
        s1.age = 10;
        
        Student s2 = new Student();
        s2 = s1;
        
        s2.age++;
        
        System.out.println(s1.age);//11
            
    }
    
}

//定义一个学生类
class Student{
    String name;
    int age;
}

this关键字

this.
this:这个的意思 只的是当前对象 谁调用我 我就代表谁
作用:用于解决成员变量和局部变量重名问题
案例1public class Demo{
    public static void main(String[] args) {
        Student s = new Student();
        //System.out.println(s.age);//20
        s.speakAge();//18    
    }
    
}

//定义一个学生类
class Student{
    String name;
    int age = 20;

    public void speakAge() {
        int age = 18;
        System.out.println(this.age);
        
    }
  =====================================================================================  
  案例2:解决构造方法为对象赋值时 解决参数名和成员变量名 重名问题导致的赋值失败 
  public class Demo{
    public static void main(String[] args) {
        Student s = new Student("桑邦好爽",56);
        System.out.println(s.name);//
        System.out.println(s.age);
        
        
    }
    
}

//定义一个学生类
class Student{
    String name;
    int age;

    public  Student(String name,int age){
        this.name = name;
        this.age = age;
    }
    
    
}

在这里插入图片描述

this()
this(参数)
作用:用于在构造方法中调用其他的构造方法  
案例1public class Demo{
    public static void main(String[] args) {
        Student s = new Student("桑邦好爽",17); 
    }
    
}

//定义一个学生类
class Student{
    String name;
    int age;
    public  Student() {
        System.out.println("无参被调用");
        //为对象初始化的一些代码
    }
    public  Student(String name) {
        this();
        System.out.println("一个参数构造被调用");
        
        this.name = name;
    }
    
    
    
    public  Student(String name,int age){
        this();
        System.out.println("两个参数的构造被调用");
        
        this.name = name;
        this.age = age;
    }
  
}
========================================================================================
案例2public class Demo{
    public static void main(String[] args) {
        Student s = new Student("桑邦好爽",17); 

        System.out.println(s.name);
        System.out.println(s.age);
        
        
    }
    
}

//定义一个学生类
class Student{
    String name;
    int age;
    public  Student() {
        System.out.println("无参被调用");
        System.out.println("1000行代码被执行");
        
        //为对象初始化的一些代码
    }
    public  Student(String name) {
        this();
        this.name = name;
         System.out.println("一个参数构造被调用");
    }
    
    
    
    public  Student(String name,int age){
        this(name);
        this.age = age;
         System.out.println("两个参数的构造被调用");
    }
  
}
========================================================================================
案例3:注意构造方法之间不可以递归调用
public class Demo{
    public static void main(String[] args) {
        Student s = new Student("桑邦好爽",17); 

        System.out.println(s.name);
        System.out.println(s.age);
        
        
    }
    
}

//定义一个学生类
class Student{
    String name;
    int age;
    /*public  Student() {
        System.out.println("无参被调用");
        System.out.println("1000行代码被执行");
        
        //为对象初始化的一些代码
    }*/
    public  Student(String name) {
        this(name,23);
        this.name = name;
         System.out.println("一个参数构造被调用");
    }
    
    
    public  Student(String name,int age){
        this(name);
        this.age = age;
         System.out.println("两个参数的构造被调用");
    }
  
}
====================================================================================
//自己之间的递归调用
public class Demo{
    public static void main(String[] args) {
        Student s = new Student("桑邦好爽",17); 

        System.out.println(s.name);
        System.out.println(s.age);
        
        
    }
    
}

//定义一个学生类
class Student{
    String name;
    int age;
    /*public  Student() {
        System.out.println("无参被调用");
        System.out.println("1000行代码被执行");
        
        //为对象初始化的一些代码
    }*/
    /*public  Student(String name) {
        this(name,23);
        this.name = name;
         System.out.println("一个参数构造被调用");
    }
    */
    
    public  Student(String name,int age){
        this(name,age);
         System.out.println("两个参数的构造被调用");
    }
  
}
========================================================================================
案例4this()和this(参数)必须放在构造方法的第一行语句
public class Demo{
    public static void main(String[] args) {
        Student s = new Student("桑邦好爽",17); 

        System.out.println(s.name);
        System.out.println(s.age);
        
        
    }
    
}

//定义一个学生类
class Student{
    String name;
    int age;
    public  Student() {
        System.out.println("无参被调用");
        System.out.println("1000行代码被执行");
        
        //为对象初始化的一些代码
    }
    public  Student(String name) {
        this();
        this.name = name;
         System.out.println("一个参数构造被调用");
    }
    
    
    public  Student(String name,int age){
   
        System.out.println("两个参数的构造被调用开始");
        this(name);//这是错误的  必须放在第一行
       
        this.age = age;
         System.out.println("两个参数的构造被调用结束");
    }
  
}
=========================================================================================
案例5:一个构造方法中只能有一个this()或者this(参数)语句
public class Demo{
    public static void main(String[] args) {
        Student s = new Student("桑邦好爽",17); 

        System.out.println(s.name);
        System.out.println(s.age);
        
        
    }
    
}

//定义一个学生类
class Student{
    String name;
    int age;
    public  Student() {
        System.out.println("无参被调用");
        System.out.println("1000行代码被执行");
        
        //为对象初始化的一些代码
    }
    public  Student(String name) {
        this();
        this.name = name;
         System.out.println("一个参数构造被调用");
    }
    
    
    public  Student(String name,int age){
        this(name);
        this();//这是错误的  必须放在第一行
        System.out.println("两个参数的构造被调用开始");
       
       
        this.age = age;
         System.out.println("两个参数的构造被调用结束");
    }
  
}
======================================================================================
案例6this()和this(参数)只能定义在构造方法中
public class Demo{
    public static void main(String[] args) {
        Student s = new Student("桑邦好爽",78);
        s.m2();
    }
    
}

class Student{
    //属性
    String name ;
    int age;
    //无参构造
    public  Student() {
        /*

        10000行代码
        */
        System.out.println("10000行代码");
        
        System.out.println("无参构造");
        
    }
    
    
    //一个参数构造、
    public  Student(String name) {
        this.name = name;
        System.out.println("一个参数的构造");
        
    }
    
    
    //两个参数构造
    public  Student(String name ,int age) {
        this(name);
        this.age = age;
        System.out.println("两个参数的构造");
        
    }
    
    //定义一个普通的方法
    public void m2() {
        this();//空参构造
    }  
}

封装

private:私有的  被private修饰的属性只能在本类访问

封装的步骤:
	1.私有属性 就是将属性用private修饰
	2.对外提供公开的get()  和 set() 方法
关于set和get方法的规范
方法名:set属性名  get属性名   例如 setAge(数据类型  参数名)  getAge()
    参数名和属性名一致
    eg: 标准的写法  小心挨打
    //set方法
    public void setAge(int age){
    	this.age = age;
    }
    
	//get方法
    public int getAge(){
    	return this.age;
    }
案例:
public class Demo{
    public static void main(String[] args) {
        Student s = new Student();
        s.setAge(50);
        System.out.println(s.getAge()); 
    }
    
}

class Student{
    //属性 设置为私有  private 私有的  
    private String name;
    private int age;

    //普通的方法 设置属性值
    public void setAge(int age) {
        if (age<0||age>120) {
            System.out.println("非法数据 赋值失败");
            
        } else{
            this.age = age;
        }
    }

    //用于获取属性值
    public int getAge() {
        if (age>30) {
             System.out.println("这是我虚假的年龄");
            return 18;
        } else{
            System.out.println("这是我真实的年龄");
            return this.age;
        }
        
        
    }
    
    
    
    
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值