Java毒丸对象_java 面向对象 封装

一、private(私有化)

作用: 保护数据安全

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

注意:

1、setAge  参数

2、getAge  返回值

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

例子

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.wt.day06.Demon04;2

3 importcom.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;4

5 public classPerson {6 //成员变量

7 String name;8 private intage;9 private booleansex;10

11 public void setAge(intage) {12 if (age > 0 && age < 100){13 this.age =age;14 } else{15 System.out.println("数据不正确");16 }17

18 }19

20 public intgetAge() {21 returnage;22 }23

24 public void setSex(booleansex) {25 this.sex =sex;26 }27

28 public booleanisSex() {29 returnsex;30 }31

32 //成员方法

33 public voidinfo(){34 System.out.println("大家好,我叫" + name + ", 性别"+ sex +",我今年" + age + "岁!!!");35 }36 }

Person.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.wt.day06.Demon04;2

3 public classDemon04Stu {4 public static voidmain(String[] args) {5 Person per = newPerson();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的内存地址和调用该方法的对象的内存地址一样

例子

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.wt.day06.Demon05;2

3 public classPerson {4 privateString name;5

6 public voidsetName(String name) {7 this.name =name;8 System.out.println(this);9 }10

11 publicString getName() {12 returnname;13 }14 }

Person.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.wt.day06.Demon05;2

3 public classDemon05Per {4 public static voidmain(String[] args) {5 Person per = newPerson();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

例子

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.wt.day06.Demon06;2

3 public classStudent {4 //私有化 成员变量

5 privateString name;6 private intage;7 private booleansex;8

9 //无参构造方法

10 publicStudent() {11 }12 //全参构造方法

13 public Student(String name, int age, booleansex) {14 this.name =name;15 this.age =age;16 this.sex =sex;17 }18

19 publicString getName() {20 returnname;21 }22

23 public voidsetName(String name) {24 this.name =name;25 }26

27 public intgetAge() {28 returnage;29 }30

31 public void setAge(intage) {32 this.age =age;33 }34

35 public booleanisSex() {36 returnsex;37 }38

39 public void setSex(booleansex) {40 this.sex =sex;41 }42 }

Student.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.wt.day06.Demon06;2

3 public classDemon06Stu {4 public static voidmain(String[] args) {5 //无参构造方法

6 Student stu1 = newStudent();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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值