Java_SE_第十讲:面向对象之封装续

Terwer Java SE评论247字数 925阅读3分5秒阅读模式
  1. 类中的属性又叫做成员变量member variable),属性用英文表示为 property 或者
    attribute
  2. 对象(Object)又叫做实例(Instance)。生成一个对象的过程又叫做实例化。
  3. 命名约定

    a) 类:首字母大写,如果一个类名由多个单词构成,那么每个单词的首字母都大写,
    中间不使用任何的连接符。比如 Person 类,MemberTest 类。文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

    b) 方法:首字母小写。如果一个方法由多个单词构成,那么第一个单词的所有字母全
    都小写,从第二个单词开始,每个单词的首字母大写。比如 addaddThreeInt文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

    c) 属性:命名约定与方法相同。比如 ageageOfPerson文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

  4. 属性需要定义在类中,又叫做成员变量;而定义在方法中的变量叫做局部变量。
  5. 如何定义属性?文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

    class 类的名字{
     修饰符 val 属性名称
    }
    
    public class Person{
     修饰符 类型 属性名称;
    }
    

    如何使用属性?与方法一样,使用.运算符。首先需要生成类的实例,然后使用实例 +”.”
    的方式来使用属性。文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

    比如:文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

    val person = Person()
    person.age
    
    Person person = new Person();
    person.age
    
  6. 局部变量使用前必须要声明并赋初值;成员变量使用前必须要声明,但可以不赋初值。
  7. 成员变量与局部变量的联系与区别:

    a) 无论是成员变量还是局部变量,使用前都需要声明(定义)。文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

    b) 对于局部变量来说,使用前必须要初始化;对于成员变量来说,使用前可以不初
    始化。如果没有初始化成员变量就开始使用,那么每个类型的成员变量都有一个
    默认的初始值文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

    i. byteshortintlong 类型的初始值为 0文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

    ii. floatdouble 类型的初始值为 0.0文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

    iii. char 类型的初始值 ‘\u0000’文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

    iv. boolean 类型的初始值为 false文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

  8. 引用类型(reference type):引用类型是用在对象上的。一个对象可以被多个引用所指
    向,但同一时刻,每个引用只能指向唯一的一个对象。如果一个对象被多个引用所指向,
    那么无论哪个引用对对象的属性进行了修改,都会反映到其他的引用当中。

文章更新历史文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

2022/05/08 feat:新增 Kotlin 语法支持。文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

2022/05/08 fix:修改备注。 文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html

文章源自浅海拾贝-https://blog.terwergreen.com/java-object-oriented-encapsulation-continued.html
相关文章
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: