Basic Java knowledge sorting (1) Object-Oriented Programming-encapsulation sorting and object-oriented Encapsulation

Source: Internet
Author: User
Tags float double

Basic Java knowledge sorting (1) Object-Oriented Programming-encapsulation sorting and object-oriented Encapsulation

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

2. An Object is also called an Instance ). The process of generating an object is called Object Instantiation.

3. Naming Conventions in Object-Oriented Programming:

(A) Class: the first letter is capitalized.Class NameComposed of multiple words, the first letter of each word must be capitalized, and no connector is used in the middle, such as the Person class or the MemberTest class.

(B) method: lowercase letters. If a method consists of multiple words, all the letters of the first word are in lowercase, starting from the second word, and the first letter of each word is in uppercase, for example: add, ageOfPerson

(C) attributes: naming conventions and methods are identical, for example, age ageOfPerson

4. the attribute needs to be defined in the class, which is also calledMember variablesThe variable in the definition method is calledLocal variable

5. How to define attributes:

Public class Person

{

Modifier type attribute name

}

The method of using attributes is the same as that of the method. The. Operator is used to generate an instance of the class, and then the instance. method uses attributes.

Person person = new Person ();

Person. age = ...;

Public class Person

{

Int age = 20; // note the position of the Class Attribute

Public static void main (String [] args)

{

Person person = new Person ();

System. out. println (person. age );

}

}

 

6. Differences between member variables and local variables:

Before using local variables, declare and assign the initial values:

Declare the member variables before using them, but do not assign the initial values;

Differences and relationships between member variables and local variables:

(A) Whether it is a member variable or a local variable, it must be declared before use:

(B) for local variables, Initialization is required before use. For member variables, Initialization is not allowed before use. If the member variables are not initialized and used, each type of member variable has a default initial value.

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

The initial value of the ii float double type is 0.0.

Iii. The initial value of the char type is '\ u0000'

The initial value of the iV boolean type is false.

7. The reference type is used on objects.

Person person = new Person (); person is a reference type rather than an object

[Note] an object can be pointed to by multiple references.

However, at the same time, each reference can only point to a unique object. If an object is pointed to by multiple references, no matter which reference modifies the attributes of the object, will be reflected in other references

Example: people. java

The allocation of referenced variables is generated in the memory on the stack. Objects in Java cannot be operated directly by referencing them.

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 );

}

 

}

First, allocate a variable of reference type of people to the stack memory. This variable will point to an object allocated on the heap memory age = 20;

Then, the stack memory will allocate another application-type variable and point to the object on the heap memory to change its value to 30. So the answer is that the age1 = 20 age2 = 30 method is complete. after, all code about these methods will be recycled by the garbage collector, so the parameters in this method will disappear.Drop

When executing the program, you can reference multiple objects, but only one object.

Think about public void change (People people)

{

People = new People ();

People. age = 30;

}

The result is that both of them are 20.

9

(1) If a class contains multiple attributes and methods, each object in the class has its own attributes, but no matter how many objects in a class, these objects share the same method.

(2) Java does not reference this type. in C # Or C ++, it is possible to pass this argument.

Passing method parameters in Java: For passing method parameters in Java, all values (pass by value) are transmitted regardless of native data type or referenced data type) if the passed parameter is a reference type, it is only the address of the object that is passed

 

(3) What type of reference can point to what type of object. For example, a reference of the people type can point to an object of the People type, but cannot point to another object. For example, a reference of the People type can point to an object of the bird type.

People people = new People (); // correct

10 Constructor is used to initialize object attributes. The Constructor has the following features:

(1) The constructor name must be exactly the same as the class name, including case sensitivity.

(2) The constructor does not return values, and even void does not appear.

(3) If a class is not declared as a constructor for the class, the java compiler automatically adds a constructor with no parameters and the method is null to the class (the Sub-method is called the default constructor)

(4) If a constructor is declared for the class when defining a class, the Java compiler will not add constructor for the class.

(5) The constructor of the class cannot be explicitly called. The constructor is usually called using the new class implicit sub-call keyword.

People p = new People ();

After this statement is executed, the memory may occur:

(A) open up space for objects

(B) construct a call class

(C) return the address of the Class Object

11 default constructor: the constructor has no parameters and is empty.

12 when the new method is used to generate an object, the parentheses () below indicate the parameter list of the constructor. If the constructor does not accept the parameter, the content in the parentheses is null, if the constructor receives parameters, the actual parameters in the parentheses must be consistent with the formal parameters in the constructor definition (the number of parameters is consistent, the parameter types are consistent, and values are assigned one by one 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 the 0 age. The default value is 0.

 

=======================================Think about the execution results of the following program: ++

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)

{

Age = 40;

}

}

The class name adopts the camper ID: AddThreeInt

The first letter of the method name must be lowercase addThreeInt (int a, int B)

The attributes and methods are the same. Do not underline them.

 


Object-Oriented Programming encapsulates data and () together

Object-Oriented Programming encapsulates data and (methods) together

The traditional process-oriented programming method ignores the internal relationship between data and operations. The data in the program is separated from the methods used to operate them, object-Oriented Programming Technology encapsulates the data to be processed and the methods to process them, forming a unity-object. The program uses an object model to model things in the real world. In this way, the structure of the spatial model is consistent with that of the problematic spatial model. The use of object-oriented methods to solve problems is more in line with human thinking methods.
 
Learning basics of JAVA Object-Oriented Programming

The video tutorial "Java from getting started to proficient" is recorded by Sun Xin (author's website: www.sunxin.org). The tutorial is easy to understand and has a comprehensive set of content. It guides Java beginners from getting started to being proficient, quickly grasp the Java programming language.

Note: to learn this tutorial, you need to learn basic programming and development knowledge, such as variables, statements, arrays, and loops. In this way, you can learn this video set to easily and quickly master Java development.

Tutorial URL: www.pconline.com.cn/..5.htmlreference information: www.pconline.com.cn/...5.html

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.