Dark Horse programmer _ & lt; Object-oriented (object, encapsulation, satic, constructor, code block, this) --- 01 &

Source: Internet
Author: User

1. Object-oriented 1. Summary programs are divided into process-oriented and object-oriented processes: Focus on a process, which is actually a function. We call programs, which is equivalent to executors. Object-oriented: a mode of thinking that regards everything as an object. An object has its own characteristics and functions. Then we can call the corresponding attributes and behaviors of the object. We are the conductor. Everything is an object. All things to be taken as an object are called attributes and behaviors. For example, we can regard a door as an object, so its attributes are long and wide, and the door plug..., and the behavior is open and closed.... 2. class is used to represent the description of real things. The members in the class are divided into member variables and member functions. Objects are actually things and individuals. The attributes of a transaction correspond to the variables of the class, and the behavior of the transaction corresponds to the functions of the class. The same object has different types of attributes. Therefore, we need to create an object to specify the features of the object and create an object using the new class name. There are three main features: encapsulation, inheritance and polymorphism, for example: [java]/*** class for describing a Person * // *** class for describing a Person */public class Person {private String name; // attribute name private int age; // attribute age public Person (String name, int age) {// constructor this. name = name; this. age = age;} public void say () {// speaking behavior function} Person p = newPerson ("zhansgan", 23); // p is a Person, A metaphor for an object is that in a car manufacturing plant, the car drawing is a class, and the car produced according to the drawing is an object. 3. member variables and local variables: the range is in the method, there is a stack in the memory, the member variable: is applied to the entire class, there is a heap in the memory. Member variables can be called by classes or objects. 4. anonymous object format: new Class Name () or new class name (parameter) usage: first, when an object value needs to be used once and is called as a member, the anonymous object is used, after use, the memory will be released. Second, it is passed as an actual parameter. 2. Encapsulation: encapsulates the public features of an object. It exists in a class and only provides public methods. Advantages: ease of management, increased reusability, and improved security. Principle: hides unnecessary attributes and provides external access methods. When using this function, you can directly use it without knowing how it is encapsulated. The same function method can be called multiple times. This is reusability. 3. constructor features: The method name and class name are consistent, and no return value exists. The function body cannot contain the final sentence (break, continue) and return content. When creating an object, you must call the corresponding constructor to initialize the property. Each class has a default no-argument constructor, so you do not need to write the constructor or customize the constructor. If you have defined the constructor, then the default constructor becomes invalid and the User-Defined constructor is called directly. Constructors can be overloaded and have different parameter types. Constructor: when an object is created, the corresponding constructor is called and only one object is executed. General method: an object call or a class call (satic). An object calls a general method multiple times. [Plain]/*** class describing a Person */public class Person {private String name; // attribute name private int age; // attribute age public Person () {// No parameter constructor System. out. println ("No parameter constructor");} public Person (String name, int age) {// constructor with parameters this. name = name; this. age = age; System. out. println ("constructor with Parameters");} public void say () {// speaking behavior function} public static void main (String [] agrs) {Person p = new Person (); Person p1 = new Person ("zhansan ", 23) ;}} Result: No parameter constructor has a parameter constructor. 4. The code block uses a piece of code enclosed. Different positions can be divided into: common code block: A code block defined in the method. Construct a code block: initializes all objects. The features of all objects can be initialized in this code block. The constructor initializes the specified object. As long as an object is created, the constructed code block is executed and takes precedence over the constructor. Static code block: A block is declared with the static keyword and executed only once. It takes precedence over the block construction. Load as the class is loaded. Synchronous Code block: A code block declared using the synchronized keyword. It is called a synchronous code block. Format: synchronized (synchronization object) {} [java]/*** class describing a Person */public class Person {private String name; // attribute name private int age; // attribute age {System. out. println ("construct code block");} static {System. out. println ("static code block");} public Person () {// No parameter constructor System. out. println ("No parameter constructor");} public void say () {System. out. println ("common code block") ;}} public void show () {synchronized (this) {System. out. println ("synchronous code block") ;}} public static void Main (String [] agrs) {Person p = new Person (); p. show (); p. result: The static code block construction code block has no parameters. The constructor synchronizes the common code block of the code block. The result shows that the static code block takes precedence over the constructed code block. The construction code block takes precedence over the construction method execution. Common Code blocks and synchronous code blocks are defined in methods without sequential order. 5. this keyword this indicates the objects in this class. As long as the methods in this class are called in this class, this is called. The object calls the method or attribute in the class. this indicates the object. [Java]/*** class describing a Person */public class Person {private String name; // attribute name public Person (String name) {this. name = name;} public void say () {show (); // It can also be this. show (). this is omitted} public void show () {System. out. println ("show ()" + name); // It can also be this. name, this is omitted} public static void main (String [] agrs) {Personp = new Person ("zhangsan"); p. result: show () Zhang San this keyword is used in the constructor. To call constructor, write this (parameter) in the constructor ); write parameters if any. No parameter is required. This statement must be written in the first line of the constructor. [Java] public class Person {private String name; // attribute name public Person () {System. out. println ("No parameter constructor");} public Person (String name) {this (); // call this. name = name; System. out. println ("constructor with Parameters");} public static void main (String [] agrs) {Person p = new Person ("James");} result: no-argument constructor with parameters 6. members modified by the static keyword can be directly called by class names or objects. However, this call cannot be used in the class because of the loading order. 6.1 features: 1. static attributes and methods are loaded with classes and disappear with the disappearance of classes. 2. takes precedence over the existence of objects. 3. shared by all objects 4. Call methods are called by class names. 5. Typical use cases can define counter 6.2 static method items static methods can only call static properties and methods! Non-static methods can access static and non-static members. This keyword cannot be defined in static methods. Because static methods take precedence over objects, this is not allowed. 6.3. Advantages and Disadvantages: object sharing saves space and can be called directly by class names. Disadvantages: access restrictions due to long lifecycle. (Static, although good, can only access static) The last main function is static. [Java] public class Demo {private static int count = 0; // static variable public Demo () {addCount ();} public static void addCount () {count ++ ;} public String show () {return "this is the" + count + "run this program";} public static void main (String [] agrs) {for (int I = 0; I <5; I ++) {Demo d = new Demo (); System. out. println (d. show () ;}}result: This is 1st run this program it is 2nd run this program it is 3rd run this program it is 4th run this program it is 5th run this program it is run this program 7. the main function is a special function, which is called by virtual machines as the entry of the program.. Public: indicates that the function has the highest access permission. Static: indicates that the function is loaded with the class, and Void: The main function has no return value. Main: Not a keyword, but a special word, which can be recognized by jvm. String [] arr: The parameter type is a String escape array. The main function is in a fixed format and is recognized by jvm. The newString [0] array passed in by the main function. It is an empty string array 8. static use of static variables: when shared data exists in the object, the data is modified statically. When the object has special data, it must be defined as non-static and exists in the heap memory. Static functions. When the function does not access non-static data (Object-specific data) static applications, the common functions are encapsulated into one piece. You do not need to call objects. Meanwhile, the constructor is defined as private to prevent other users from creating unnecessary objects. This is more rigorous. [Java] blic class MathTool {private MathTool () {} public static int max (int a, int B) {// returns a> B? A: B;} public static int min (int a, int B) {// returns a> B? B: a ;}} public class Text {public static void main (String [] args) {System. out. println ("maximum value:" + MathTool. max (12, 32); System. out. println ("minimum value:" + MathTool. min (12, 32) ;}result: Maximum Value: 32 minimum value: 12

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.