博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Java in NetBeans] Lesson 06. Custom classes
阅读量:5050 次
发布时间:2019-06-12

本文共 1547 字,大约阅读时间需要 5 分钟。

这个课程的参考视频和图片来自。

    主要学到的知识点有:

  • Constructors: A special method called when an object of the class is created
  • property pattern and encapsulation(封装): hide the implementation details from the user, so when the class is been inherited, only the methods, but not the members. 
  • this: simply refers to the current class. 
  • Also allow us to call other constructor in one constructor
public class Foo {    private int _x;  // if class members, and not public, start with underscore.    public Foo()     {        this(1);    }    public Foo(int x)    {        _x = x;    }}

 

  • Overload: The method with different parameter but same method name. (for example, the constructors. Java will automatic search for the constructors that fits the parameters passed in)

             e.g. Foo foo = new Foo(8); will automatic search for the second constructor "public Foo(int x)".

  • Every class should have a constructor. But the body can be empty. 
  • Declare variables as private as possible. Can create getter and setter for the variables to control access to private variables.  based on the encapsulation(封装) concept. 
  • Initialize all private variables in constructor. (if not, make them final)
  • this disambiguates method parameters from private members, but will name the members with _(unless it is public). Below is an example without "_".
public class Foo {    private int x;    public Foo()     {        this(1);    }    public int setX(int x)    {        this.x = x;    }}
  • Use get/set methods to control access to private variables. 

转载于:https://www.cnblogs.com/Johnsonxiong/p/10086486.html

你可能感兴趣的文章
android调试debug快捷键
查看>>
【读书笔记】《HTTP权威指南》:Web Hosting
查看>>
Inoodb 存储引擎
查看>>
数据结构之查找算法总结笔记
查看>>
Linux内核OOM机制的详细分析
查看>>
Android TextView加上阴影效果
查看>>
Requests库的基本使用
查看>>
C#:System.Array简单使用
查看>>
「Foundation」集合
查看>>
二叉树的遍历 - 数据结构和算法46
查看>>
类模板 - C++快速入门45
查看>>
RijndaelManaged 加密
查看>>
Android 音量调节
查看>>
windows上面链接使用linux上面的docker daemon
查看>>
Redis事务
查看>>
Web框架和Django基础
查看>>
python中的逻辑操作符
查看>>
HDU 1548 A strange lift (Dijkstra)
查看>>
每天一个小程序—0005题(批量处理图片大小)
查看>>
IIS的各种身份验证详细测试
查看>>