Classes and objects

Source: Internet
Author: User
Tags define abstract

I. Classes and objects

(1) Class: A class is a mold that determines the characteristics (attributes) and behavior (methods) that an object will have.

Class: A class is a collection of a set of objects that have the same properties and methods.

(2) Object

Create object: Class Name Object name = new class name ()

Working with objects: objects. Properties/Objects. Method Name ()

(3) member variables and local variables

Member variables: defined in a class to describe what the object will be. (scopes are visible within the entire class)

Local variables: defined in a method of a class to temporarily save data in a method. (Scope is limited to the method that defines it)

Difference:

① Scope not used

Local variables have higher precedence when ② two types of variables with the same name

③ initial values are different: Java gives the member variable an initial value and does not give the local variable the initial value

(4) Constructor method: Defines a method in Java to initialize an object.

① creates a new object using the new+ construction method.

The ② name is the same as the class name and has no return value.

③ when no construction method is specified, the system automatically adds an argument-free construction method.

④ when a constructor method is specified, no parameter is automatically added to the constructor method, no matter whether it is a constructor with a parameter or no argument.

The ⑤ constructor method can be overloaded: The method name is the same, but the parameters are different, and the corresponding method is automatically selected according to the different parameters when called.

⑥ construction method can not only assign values to the properties of an object, but also ensure that the property of the object is assigned a reasonable value (adding judgment in the constructor).

(5) static variable (also called class member)

① it belongs to the entire class, not to an object, and is shared by all objects of the class.

② static members can be accessed directly using the class name or by using the object name.

③ static members belong to the entire class, and when the system first uses the class, it allocates memory space until the class is unloaded for resource reclamation.

(6) Static Statics method (also called class method)

Static members in the same class can be called directly in ① static methods, but non-static members cannot be called directly.

② if the system calls a non-static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object.

③ in the normal member method, you can directly access the same non-static variables and static variables.

Non-static methods cannot be called directly in ④ static methods, and non-static methods need to be accessed through objects.

(7) Initialization block

① Ordinary initialization blocks

{

xxx = xxx;

}

② Static initialization block

static {

xxx = xxx;

}

③ execution Order: Static initialization block is executed first--normal initialization block---construction method

Second, the package

(1) Concept: To hide some of the information of a class inside a class, not to allow external programs to access directly, but through the class provided by the method class to implement the operation and access to the hidden information.

(2) Benefits: Access to data only with the prescribed method, hidden class implementation details, easy to modify and implement.

(3) Implementation steps

① Modifying the visibility of a property: set to Private

② creating Getter/setter methods: For reading and writing properties

③ adding attribute control statements to the Getter/setter method: Judging the legality of attribute values

(4) Packages in Java

① role: Managing Java files, resolving conflicts with files of the same name

② must be placed in the first line of the Java source program, with "." Between package names. Number separated

Use of the ③ package: You can use the Import keyword in a file with other files in the class, Java package naming is all lowercase letters

(5) Access modifiers in Java: You can modify the access scope of properties and methods

①private: can only be accessed and used in this class.

② default: Can be accessed and used in this class and in the same package.

③protected: Can be used in this class, in the same package, sub-class.

④public: Used in this class, in the same package, in subclasses, in other classes.

(6) The This keyword in Java

Represents the current object

This. Property: Manipulate the properties of the current object

This. Method: Method that invokes the current object

(7) Inner class: A class defined in another class.

Role:

The ① inner class provides a better encapsulation that hides inner classes within external classes and does not allow other classes in the same package to access the class.

The methods of the ② inner class can access all the data of the external class directly, including the private data.

The functionality implemented by the ③ inner class can be implemented using an external class, but it is sometimes more convenient to use internal classes.

Classification: Member inner class, Static inner class, method inner class, anonymous inner class.

(8) member inner class (also called Ordinary inner class)

The ① inner class is defined inside an outer class and is equivalent to the position of a member variable of an outer class, and an inner class can use any access control character.

The methods in the ② inner class can directly access the data in the external class without being affected by the access control.

After ③ defines the member inner class, you must use an external class object to create the inner class object, rather than go directly to the new inner class object.

That is, the inner class object name = External class object. New Inner Class ()

④ external classes cannot use the members and methods of an inner class directly, create an object of an inner class, and then access its member variables and methods through an object of the inner class.

⑤ if an external class and an inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variables of the external class, use the external class. this. Object name

(9) Static inner class (internal class for static decoration)

① static inner classes cannot directly access non-static members of external classes, but can be accessed through the "New external Class (). Member" method

② if the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. static member; If the static member of the outer class is not the same as the member name of the inner class, it can be called directly through the member name.

③ you create an object of a static inner class, you do not need an object of the outer class, you can create it directly: Inner class object name = New inner class ();

(10) Method inner class

The inner class of the method is the inner class defined in the method of the outer class, and the inner class of the method is visible only within the method, that is, it can be used only inside the method.

Note: Because the method inner class cannot be used outside the methods of the outer class, the method inner class cannot use the access control modifier and the static modifier.

Iii. inheritance

(1) A relationship between class and class at the time of inheritance, is a relationship of "is a"

(2) Inheritance in Java single inheritance, only one parent class

(3) class subclass extends parent class

(4) Rewriting of methods

The ① subclass overrides a method inherited from a parent class, and the method of the subclass is called first when the method is called.

The ② return value type, method name, parameter type, and number are the same as the method inherited by the parent class, which is called the override of the method.

(5) initialization Order of inheritance

① initializes the parent class and initializes the subclass

The initialization of the ② property is performed before the constructor method.

The ③ property of the parent class is initialized--the constructor of the parent class--the property of the child class---the constructor of the child class

(6) Final

① to modify classes, methods, properties, and variables

②final decorated class, the class does not allow inheritance

③final modified method, the method is not allowed to be overwritten (overridden)

④final The property is modified, the properties of the class are not implicitly initialized (the initialization property of the class must have a value) or be assigned in the constructor method (only optional)

⑤final modifies a variable, the value of the variable can be assigned only once, which becomes a constant

(7) Super keyword

Used inside an object, you can represent the parent class

(8) The object class is the parent class of all classes, and if a class does not explicitly identify inheriting another class using the extends keyword, the class inherits the object class by default.

Iv. polymorphic

Inheritance is the basis for polymorphic implementations.

(1) Reference polymorphism: a reference to the parent class can point to the object of this class/the reference to the parent class can point to the object of the child class.

(2) Method polymorphism: When you create this type of object, the method that is called is the method that is overridden by the subclass or the method that is inherited, when it is used for this class of methods/To create a subclass object.

(3) Reference type conversions

①-Up type conversions: implicit/automatic type conversions, small types to large types of conversions. (No Risk)

② down type conversions: coercion type conversions, which are large types to small types. (with Risk)

(4) Abstract class

① scenario: In some cases, a parent class simply knows how its subclasses should be, but does not know exactly how these subclasses implement these methods.

② abstracts an abstract class from multiple classes with the same characteristics, taking this abstract class as a template for subclasses, thus avoiding the arbitrariness of sub-class design.

③ restriction subclasses must implement some methods, but do not pay attention to implementation details.

④abstract Defining abstract Classes

⑤abstract define abstract methods, only declarations, do not need to implement

⑥ classes that contain abstract methods are abstract classes

⑦ Abstract classes can contain ordinary methods, or there can be no abstract method

⑧ Abstract classes cannot be created directly, you can define reference variables

(5) interface

The ① interface can be understood as a special class that consists of global constants and common abstract methods.

The ② class is a concrete implementation body, and the interface defines the specification that a batch of classes need to follow, the interface does not care about the internal data of these classes, and does not care about the implementation details of the methods in these classes, it only stipulates that certain methods must be provided in these classes.

The basic syntax for the ③ interface definition:

[Modifier] Interface Interface Name [extends parent interface 1, parent interface 2]

{

0 to more constant definitions ...

Definitions of 0 to multiple abstract methods ....

}

A class in ④java can have only one parent class and is not flexible enough to increase flexibility by implementing multiple interfaces.

The properties in the ⑤ interface are constants, which are automatically added even if the public static final modifier is not added when defined.

The method in the ⑥ interface can only be an abstract method, and the system will automatically add public abstract

(6) UML

① Concept: A unified Modeling language or standard modeling language that supports graphical languages for modelling and software system development, providing modeling and visualization support for all phases of software development.

UML diagrams commonly used in ②

Use case diagram: the ability to visualize how the system meets the collected business rules and the specific user requirements information.

Sequence diagram: Used to place the sequence of interactions occurring, showing these interactions between objects.

Class Diagrams: UML Class Diagrams, business logic, and all supporting structures are used to define the entire code structure.

Classes and objects

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.