Java的封装

目录


1.构造方法

1.1 构造方法定义

        构造方法是与类名同名,并且不设置返回值符号,在调用类时自动被调用的方法。可以理解为类的初始化方法,实际上定义类时会存在一个默认的构造方法,但不具有方法体,也就是实际上无任何效果。

1.2 构造方法语法

public class Demo01 {
    public Demo01(){
        //没有返回值的标识符
        //方法名和类名相同
        System.out.println("你好");//构造方法的方法体
    }

1.3 构造方法的作用

        构造方法是在调用类创建对象时,自动被调用的方法,作用一般是对对象的属性进行初始化。

public class Demo01 {
    public Demo01(){
        System.out.println("你好");
    }

    public static void main(String[] args) {
        Demo01 demo01 = new Demo01();
        //创建类的对象时会直接调用该类的构造方法
    }
}

Java的封装 

2.方法的重载

2.1 方法重载的定义

        当同名函数具有传入多种参数的可能性时,则需要用到方法重载。方法重载的条件有①方法名相同②方法的参数不同(数量或类型)
        注意当访问修饰符或返回值设置不同,而参数设置相同时,无法构成方法的重载。

2.2 普通方法的重载

 

2.3 构造方法的重载

public class Test {
    public Test(){
        System.out.println("无参构造方法");
    }
    public Test(int a){
        System.out.println("单参构造方法 a:"+a);
    }
    public Test(int a, String b){
        System.out.println("无参构造方法 a:"+a +"b:"+b);
    }

    public static void main(String[] args) {
        new Test();
        new Test(1);
        new Test(1,"1");
    }
}

 

3.static

3.1 static成员的调用

public class Demo04 {
    //static
    static String name;
    public static void main(String[] args) {
        //可以不创建对象直接给static属性赋值
        Demo04.name = "旺财";
        //可以直接调用类的属性
        System.out.println(Demo04.name);//结果为“旺财”
        //给属性赋值后,是永久改变的,当创建其他对象后,其属性也会变为该值
        Demo04 demo04 = new Demo04();
        System.out.println(demo04.name);//结果为“旺财”
        Demo04 demo041 = new Demo04();
        //改变一个对象的属性,也会同时改变其他对象的属性
        //意味着这个属性是所有对象公有的
        demo041.name = "土豆";
        System.out.println(demo041.name);
        System.out.println(demo04.name);
        System.out.println(Demo04.name);
        //三个结果都是“土豆”
    }
}

3.2 static的内存空间

        简单画一下,比较简陋,如果不正确,请指教。

3.3 static 和 无static 的区别

4.封装

4.1 什么是封装

        当我们需要安全地访问对象时,例如限制给对象赋值的范围(避免数据类型的不同或者数据范围超出预计),我们就需要使用封装技术。
        封装就是将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法(getter和setter)来实现对隐藏信息的操作和访问。

4.2 封装的步骤

1.使用关键字private对类的属性进行隐藏(方法一般不用隐藏)

2.利用setter/getter方法对属性值进行操作

3.可以在方法中加入条件控制语句,进行限制

4.3 封装示例

public class Person {
    private int id;
    private String pwd;
    private String health;
    public Person(){
        System.out.println("欢迎回来");
    }
    public Person(int inputId){
        this.id = inputId;
        System.out.println("欢迎回来");
    }
    public  Person(int inputId,String inputPwd){
        this.id = inputId;
        this.pwd = inputPwd;
        System.out.println("欢迎回来");
    }
    public  Person(int inputId,String inputPwd,String inputHealth){
        this.id = inputId;
        this.pwd = inputPwd;
        this.health = inputHealth;
        System.out.println("欢迎回来");
    }

    public int getId() {
        return id;
    }

    public String getHealth() {
        return health;
    }

    public String getPwd() {
        return pwd;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public void setHealth(String health) {
        this.health = health;
    }
}

 

public class PersonTest {
    public static void main(String[] args) {
        Person person = new Person(2,"123456","健康");

        System.out.println(person.getId());
        System.out.println(person.getPwd());
        System.out.println(person.getHealth());

    }
}

4.4 this

        在使用setter方法的时候,可以发现this关键字,这是由于方法的变量名和属性名是一样的,前文提到局部变量名和成员变量名会优先使用局部变量名,this就是解决这种情况的办法。
        this关键字是表示对对象本身的引用,this可以调用对象自己的属性,方法以及构造方法。对属性和方法的调用都是使用”.”标识符进行引用,但一般调用方法时不会使用this,除非在方法重载时使用。

4.4.1 构造方法中this的使用

        对自身构造方法的调用,this()只能写在构造方法中,且需要放在第一行,而且要避免形成死循环。

public class TestThis {
    static int a;

    public TestThis(){
        System.out.println("你好");
    }
    public TestThis(int a){
        this();
        System.out.println("你好"+a);
    }
    public TestThis(String b){
        this(a);
        System.out.println("你好"+b);
    }
}
public class Test {
    public static void main(String[] args) {
        TestThis testThis = new TestThis("小明");
    }
}

 

 

版权声明:本文为博主作者:努力的泽泽原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/weixin_52071901/article/details/131028564

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2024年4月16日
下一篇 2024年4月16日

相关推荐