java 面向对象 封装

一、private(私有化)

作用: 保护数据安全

特点: 私有化成员变量,只有在改成员变量的作用域内才能访问

注意:

1、setAge  参数

2、getAge  返回值

3、当成员变量的数据类型是boolean 类型时, 设置:setSex   获取:isSex

例子

 1 package cn.wt.day06.Demon04;
 2 
 3 import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
 4 
 5 public class Person {
 6     // 成员变量
 7     String name;
 8     private int age;
 9     private boolean sex;
10 
11     public void setAge(int age) {
12         if (age > 0 && age < 100){
13             this.age = age;
14         } else {
15             System.out.println("数据不正确");
16         }
17 
18     }
19 
20     public int getAge() {
21         return age;
22     }
23 
24     public void setSex(boolean sex) {
25         this.sex = sex;
26     }
27 
28     public boolean isSex() {
29         return sex;
30     }
31 
32     // 成员方法
33     public void info(){
34         System.out.println("大家好,我叫" + name + ", 性别"+ sex +",我今年" + age + "岁!!!");
35     }
36 }
Person.java
 1 package cn.wt.day06.Demon04;
 2 
 3 public class Demon04Stu {
 4     public static void main(String[] args) {
 5         Person per = new Person();
 6         per.name = "张飞";
 7         per.setAge(-20);
 8         per.setSex(true);
 9         per.info();
10 
11         System.out.println(per.getAge()+ "我干" + per.isSex());
12     }
13 }
Demon04Stu

二、this

注意:

1、this 要在方法中

2、this指的是调用该方法的 对象

3、this的内存地址和调用该方法的对象的内存地址一样

例子

 1 package cn.wt.day06.Demon05;
 2 
 3 public class Person {
 4     private String name;
 5 
 6     public void setName(String name) {
 7         this.name = name;
 8         System.out.println(this);
 9     }
10 
11     public String getName() {
12         return name;
13     }
14 }
Person.java
 1 package cn.wt.day06.Demon05;
 2 
 3 public class Demon05Per {
 4     public static void main(String[] args) {
 5         Person per = new Person();
 6         System.out.println(per);
 7         // cn.wt.day06.Demon05.Person@1540e19d
 8         per.setName("tom");
 9         // cn.wt.day06.Demon05.Person@1540e19d
10     }
11 }
Demon05Per

三、构造方法

注意

1、public + 类命(大小写一致)

2、没有返回值

3、可以overload

4、类自带一个无参的构造方法,若有一个有参的构造方法时,自带的无参构造方法将不生效

5、构造方法,在类实例化对象 new 的时候创建

四、完成的类(intellij快捷创建和eclipse一样)

1、私有成员变量

2、构造方法(无参构造方法 + 全参构造方法)

3、get set

例子

 1 package cn.wt.day06.Demon06;
 2 
 3 public class Student {
 4     // 私有化 成员变量
 5     private String name;
 6     private int age;
 7     private boolean sex;
 8 
 9     // 无参构造方法
10     public Student() {
11     }
12     // 全参构造方法
13     public Student(String name, int age, boolean sex) {
14         this.name = name;
15         this.age = age;
16         this.sex = sex;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public int getAge() {
28         return age;
29     }
30 
31     public void setAge(int age) {
32         this.age = age;
33     }
34 
35     public boolean isSex() {
36         return sex;
37     }
38 
39     public void setSex(boolean sex) {
40         this.sex = sex;
41     }
42 }
Student.java
 1 package cn.wt.day06.Demon06;
 2 
 3 public class Demon06Stu {
 4     public static void main(String[] args) {
 5         // 无参构造方法
 6         Student stu1 = new Student();
 7         stu1.setName("tom");
 8         stu1.setAge(24);
 9         stu1.setSex(true);
10         System.out.println("姓名:" + stu1.getName() + "年龄: " + stu1.getAge() + "性别:" + stu1.isSex());
11 
12         System.out.println("================================================================");
13         // 全参 构造方法
14         Student stu2 = new Student("rose", 79, false);
15         System.out.println("姓名:" + stu2.getName() + "年龄: " + stu2.getAge() + "性别:" + stu2.isSex());
16         stu2.setSex(true);
17         stu2.setAge(8000);
18     }
19 }
Demon06Stu
原文地址:https://www.cnblogs.com/wt7018/p/12181487.html