Members of the java--class

Source: Internet
Author: User
Tags instance method

First, method overloading

In the same class, an overload is formed between two multiple methods with the same method name and a different parameter list! Overload calls the method, it is based on the arguments you pass, to determine exactly what method of overloading is called!!!

注意:1、判断形参列表是否相同时,只看形参的类型,而不管变量名2、是否重载和返回值类型没有任何的关系!3、形参列表不同:个数不同或者顺序不同都是不同4、重载其实多态中的一种情况:静态多态void speak(){System.out.println("啥都不说");}void speak(String msg){System.out.println("说:" + msg);}

The following are not overloaded

虽然参数名不同,但是类型不同,这不是重载,这是重复void speak(int a){}void speak(int b){}
Second, the construction method

Main role: Do some initialization work on the object created!!!
constructors, constructors, construction methods, Constructor

Characteristics of the construction method:

1、首先他是一个方法2、构造方法名与类名相同!!!3、构造方法没有返回值类型!!! 连void都不许写4、构造方法是在创建对象 new 类名() 的时候调用的。5、构造方法,不能通过对象.方法名()去调用。6、构造方法也支持重载!!!

There are two main types of construction methods:
1, the construction method without parameters:
A: If a class does not display any of the constructor methods that are declared, then the system automatically adds a default parameterless construction method. This default parameterless construct, without any implementation (there is no code in it).
B: If you add any of the construction methods, the system no longer adds an argument-less construction method.
2, the construction method of the parameter:

建议:写类的,总是手动添加一个无参的构造方法!!!
Third, the encapsulation of

Three main features of object-oriented: encapsulation, inheritance, polymorphism

Private it can be used to modify properties and methods. On behalf of this member is private. A private member can only be accessed in the current class, and after the class has been removed, there is no access anywhere else.

Public-modified properties and methods that can be accessed anywhere

 give private properties, provide a readable method public int getage () {return age;} public string getName () {return name;} For private properties, provide a writable method public void Span class= "Hljs-title" >setage (int a) {age = A;} public void setname (String n) {name = n;}        

Setter, Getter method
Ordinary classes typically encapsulate attributes, and then provide the appropriate setter and getter methods

Third, this keyword

Variable Access principle: the nearest principle!

1, this refers to the object of the current class
Refers to the current object, which if omitted, still refers to the member variable, you can omit.

this refers to the current object, you can call the current object's properties and methods! public void eat (string food) {System. Out.println ( "eating:" + food);} //object, instance, non-static method public void eat ( This.eat ( "bamboo"); //this position can be omitted} NOTE: If you call a method without specifying an object, the this        when swapping with your own method inside a class 

2, this can be in the construction method of the internal to call other construction methods
This (parameter)

注意:      1、用this调用构造方法的时候,不能 a调用b,b再调用a2、this调用构造方法的时候,必须放在构造方法的首行。3、一个构造方法如果没有显示的用this去调用其他构造方法或者用super去调用父类的构造方法,则这个构造方法,一定默认调用了父类的无参的构造方法。

3. This can be passed as an argument. Represents a reference to the current object.

fun(){    Person person = new Person();    person.play(this);  //可以把作为参数传递到方法的内部}
Iv. Static Keywords

Static, which can be decorated with properties and methods!!! Members that are modified by static are called static properties or static methods. (Not modified by static: Instance method, non-static method)

When modifying a property, it means that the property is static and that this property is a common resource that is shared by all objects.

When accessing static members, the most standard practice is not to recommend using objects to access static members!

Static members are accessed statically: class name. static member. When statically initialized with the loading of a class, only the first time the class is loaded into memory completes the initialization.

Never use objects to access static stuff!!

Access static always uses the class name to access it, rather than using the object to access it. Static things, already and objects do not have much of a relationship. Static things, which are property classes, all static properties and static methods, we call them: class methods and Class properties

In practice, many of the tool classes are provided with a lot of static methods to let us use!!!

静态不能访问非静态!!!非静态可以访问静态!!!在静态方法中会不会可能出现this?    不可能

Static modifier code is fast, called Static code block!

构造代码块:{System.out.println("我是代码块");}注意:先执行构造代码块再执行构造方法。 构造代码在实际没有使用价值静态代码块:static{System.out.println("我是静态代码块");}注意:静态代码块,只有类第一次加载的时候,才会调用,所以静态代码库之执行一次。 在类加载的时候,对类的静态的属性做一些初始化的工作。
Summarize:
类里面都可以放哪些东东?1、实例变量(属性)    属于对象的,只能有了对象之后才能访问。    2、静态变量(静态属性)    不属于某个具体的对象,而是属于类的。使用静态的方式去访问:    类名.静态    3、实例方法    属于对象的,只有有了对象之后才能访问。    4、静态方法    属于类。静态方法也称为类方法。 类名.静态方法。    静态能访问非静态,非静态可以访问静态!5、构造方法 构造方法只有在使用new来创建对象的时候才会执行。 6、代码块 a:构造代码块 创建对象的时候调用,而且是优先于构造方法执行! b:静态代码块 类加载到内存中的时候调用。整个生命周期中,静态代码块只执行一次。

Members of the java--class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.