Java Basic Knowledge Collation (i) object-oriented programming--encapsulation and finishing

Source: Internet
Author: User
Tags float double naming convention

The attribute in Class 1 is also called a member variable (member variable), and the attribute is in English (property) or attribute

2 Objects (object) are also called instances (Instance). The process of generating an object is called an instantiation of the object

3 Naming conventions in object-oriented programming:

Class (a): capitalized, if a class name consists of multiple words, the first letter of each word should be capitalized, the middle does not use the still connector, such as the person class, Membertest class

(b) Method: First letter lowercase, if a method is composed of multiple words, then all the letters of the first word are all lowercase, starting with the second word, the first letter of each word capitalized, for example: Add, Ageofperson

(c) attribute: The naming convention is exactly the same as the method, for example: Age Ageofperson

The 4 attribute needs to be defined in a class, also called a member variable , and the variable in the definition method is called a local variable

5 How to define attributes:

public class Person

{

Modifier Type property Name

}

Methods for using attributes: As with methods; use the. operator to first generate an instance of the class, and then make the instance. Method using Properties

Person Person=new person ();

Person.age= ...;

public class Person

{

int age=20; Note: The location where the properties of the class are written

public static void Main (string[] args)

{

Person Person=new person ();

System.out.println (Person.age);

}

}

6 The difference between a member variable and a local variable:

To declare and assign an initial value before local variables are used:

Member variables to be declared before use, but can not assign the initial value;

Differences and linkages between member variables and local variables:

(a) Both the member variable and the local variable are declared before use:

(b) For local variables: must be initialized before use, for member variables, can not be initialized before use, if you do not initialize member variables and start using, then each type of member variable has a default initial value

I byte short int the initial value of type long is 0

ii The initial value of the float double type is 0.0

The initial value of type III char is ' \u0000 '

IV the initial value of the Boolean type is False

7 Reference type (reference type) reference type is used on the object

Person Person=new person (); Person is a reference type and not an object

"Annotated" an object can be pointed to by multiple references

But at the same time, each reference can only point to a unique object, and if an object is pointed to by more than one reference, no matter which reference modifies the object's properties, it will be reflected in the other references.

Example: People.java

Reference variable assignment is generated in memory on the stack is object Java inside cannot manipulate object directly is by reference to manipulate

public class people

{

int age=20;

Public Void Change (people people)

{

people.age=30;

}

public static void Main (string[] args)

{

People people =new people ();

int age1=people.age;

System.out.println (AGE1);

People.change (people);

int Age2=people.age

System.out.println (Age2);

}

}

A variable of a people reference type is allocated on the stack memory first, which points to an object age=20 allocated on the heap memory;

then the stack of memory will be allocated an application type of the variable and point to the heap memory of this object to change his value to 30 so the answer is age1=20 age2=30 method execution, all the code about these methods will be recycled by the garbage collector, So the form that exists in this method disappears.

In this execution of the program, the reference can be multiple, but only one object

Consider if public void change (people people)

{

People=new people ();

people.age=30;

}

The result of the execution is that two are all 20.

9

(1) If a class contains multiple properties and methods, each object of that class has its own properties, but no matter how many objects a class has, they share the same method.

(2) In Java there is no reference to this type, in C # or C + + it is possible to pass a reference to this argument.

In Java, about method parameter passing: for the passing of a method parameter in Java, whether it is a native data type or a reference data type, the value passed (pass by value) is a reference type, except that the address of the object is passed

(3) What type of reference can point to what type of object, such as a reference to a people type, can point to an object of type people, but cannot point to another object such as people type points to bird

People people=new people ();//correct

10 Construction Method: Construction Method (Constructor) Construction method is used to accomplish the initialization of object properties, the characteristics of the construction method:

(1) The name of the construction method must be exactly the same as the class name including case

(2) The construction method has no return value, and even void does not appear

(3) If you do not construct a method for a class declaration when defining a class, the Java compiler automatically adds a constructor that has no parameters and the method is null for the class (the secondary method is called the default constructor method)

(4) If you declare a constructor method for a class when you define a class, the Java compiler will no longer add a constructor for the class.

(5) You cannot explicitly invoke the constructor of a class, and the constructor method is usually called by the keyword new class implicitly.

People p =new people ();

After executing this sentence, the memory will appear:

(a) Opening up space for objects

(b) Constructor method for calling a class

(c) Return the address of the object of the class

11 Default Construction Method: Constructor method without parameters and method experience empty

12 when using new to generate an object, the following parentheses () represent the argument list of the constructed method, and if the constructor does not accept the argument, the contents of the parentheses are empty, and if the constructor receives the arguments, the actual arguments in the parentheses need to be consistent with the formal parameters in the constructor definition (the number of parameters is consistent, parameter types are consistent, each assigned value in order)

public class Persontest

{

public static void Main (string[] args)

{

person person = new person ();

System.out.println (Person.age);

}

}

Class Person

{

int age;

Public person ()

{

}

}//print out the default value of 0 age is 0

======================= think of the following program execution results +++++++++++++++++

public class Paramtest
{

public static void Main (string[] args)

{

Person Person=new person ();

Person.change (person);

int age=person.age;

System.out,println (age);

int i=10;

Person.change2 (i);

System.out.println (i);

}

}

Class Person

{

int age = 20;

public void change (person person)

{

Person=new person ();

person.age=30;

}

public void Change2 (int.)

{

age=40;

}

}

Name of class with hump identification: Addthreeint

Name of method first letter to lowercase addthreeint (int a,int b)

Properties and methods are the same, do not underline in the middle

Java Basic Knowledge Collation (i) object-oriented programming--encapsulation and finishing

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.