Object-oriented,

Source: Internet
Author: User

Object-oriented,
1. object-oriented concept

  • Object-oriented is a common idea that conforms to people's thinking habits.
  • The emergence of object-oriented architecture simplifies complicated problems.
  • The emergence of object-oriented makes the executors in the process become the commanders in the object.

 

  • Object-Oriented development, design, and features
    • Development: constantly create objects and use objects and command objects to do things.
    • DESIGN: the relationship between objects is managed and maintained.
    • Object-oriented features: encapsulation, inheritance, and Polymorphism

 

Relationship between two types of objects
  • We learn programming languages to simulate more than a dozen things and implement informatization. For example, the billing system for shopping in supermarkets and the Business System for banking.
  • How do we express a thing in the real world?
    • Attribute: The description of the transaction.
    • Action: What can this thing do.
  • The most basic unit of Java is class. Therefore, we should use a class to represent things.

 

  • Class: a set of related attributes and behaviors.
  • Object: is a concrete embodiment of such a thing.
  • Example: Class-student, object-class monitor.

  • Class Definition
    • Real-world things
      • The height and weight of the specified person.
      • Learn, eat, and so on.
  • The same is true for describing things using classes in Java.
    • A member variable is a property of a thing.
    • The member method is the action of a thing.
  • A definition class is actually a member of a definition class (member variables and member methods ).

 

  • Example: Automobile
Package java006;/**/9/6 * Description: Car type */public class Car {int num; // Number of tires String color; // color/*** run */public void run () {System. out. print (num + "" + color );}}
Package java006;/***** * Description: */public class CarTest {public static void main (String [] args) {Car car = new Car (); car. num = 4; car. color = "red"; car. run ();}}
  • What is the difference between a member variable and a local variable?
    • ① Different positions in the class
      • Member variables are defined outside the class's methods.
      • Local variables are defined in the method or method declaration.
    • ② Locations in memory are different
      • Member variable in heap memory.
      • Local variable in stack memory.
    • ③ Different lifecycles
      • Member variables exist with the object and disappear with the object.
      • Local variables exist with the call of the method, and disappear with the completion of the call of the method.
    • ④ Different initialization values
      • Member variables with default initialization values.
      • There is no default initialization value for a local variable. It must be defined, assigned a value, and then used.

 

  • Memory embodiment of the parsing object
    • Example:
Package java006;/**/9/6 * Description: Car type */public class Car {int num; // Number of tires String color; // color/*** run */public void run () {System. out. print (num + "" + color );}}
Package java006;/**** * Description: */public class CarTest {public static void main (String [] args) {Car car1 = new Car (); car1.num = 4; car1.color = "red"; Car car2 = new Car (); car2.num = 4; car2.color = "black"; car1.run (); car2.run ();}}

 

    • Example:
Package java006;/**/9/6 * Description: Car type */public class Car {int num; // Number of tires String color; // color/*** run */public void run () {System. out. print (num + "" + color );}}
Package java006;/**** * Description: */public class CarTest {public static void main (String [] args) {Car car1 = new Car (); car1.num = 4; car1.color = "red"; Car car2 = car1; car1.run (); car2.run ();}}

 

  • Anonymous object
    • Definition: an object without a name is a simplified representation of the object.
    • Use Cases:
      • When an object calls a method only once.
      • Passed as the actual parameter.
    • Example 1:
Package java006;/**/9/6 * Description: Car type */public class Car {int num; // Number of tires String color; // color/*** run */public void run () {System. out. print (num + "" + color );}}
Package java006;/**** * Description: */public class CarTest {public static void main (String [] args) {Car car1 = new Car (); new Car (). run ();}}
    • Example 2:
Package java006;/**/9/6 * Description: Car type */public class Car {int num; // Number of tires String color; // color/*** run */public void run () {System. out. print (num + "" + color );}}
Package java006;/***** * Description: */public class CarTest {public static void main (String [] args) {show (new Car ());} public static void show (Car c) {c. num = 4; c. color = "black"; c. run ();}}

 

3 Encapsulation
  • Encapsulation: hides the attributes and implementation details of an object and only provides public access.
  • Advantages of encapsulation:
    • Isolate changes.
    • Easy to use.
    • Improve reusability.
    • Improve security.
  • Encapsulation principles:
    • Hide all content that does not need to be provided externally.
    • Hide all attributes and provide public methods to access them.

 

  • Example:
Package java006;/***** * Description: Human */public class Person {private int age; public int getAge () {return age;} public void setAge (int age) {if (age> 0 & age <= 200) {this. age = age ;}} public void speak () {System. out. print ("My age is:" + age );}}
Package java006;/**** * Description: */public class PersonTest {public static void main (String [] args) {Person p = new Person (); p. setAge (-1); p. speak ();}}

 

4. Constructor
  • Features:
    • The function name and class name are the same.
    • You do not need to define the type of the return value, even if there is no void.
    • No specific return value.
  • Purpose:
    • Initialize the object.
  • Note:
    • If you do not provide a constructor, the system will provide the default constructor.
    • If you provide a constructor, the system will not provide it any more.
    • You can overload the constructor.

 

  • Example:
Package java006;/*** safety /9/6 * Description: Human */public class Person {private int age; private String name; public Person () {} public Person (String name, int age) {this. setName (name); this. setAge (age);} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {if (age> 0 & age <= 200) {this. age = age ;}} public void speak () {System. out. print ("My name is:" + this. name + "My age is:" + age );}}
Package java006;/**** * Description: */public class PersonTest {public static void main (String [] args) {Person p = new Person ("Zhang San ", 26); p. speak ();}}

 

  • What is the difference between a constructor and a general function?
      • Constructor: when an object is created, the corresponding constructor is called to initialize the object.
      • General functions: objects are called only after they are created.
      • Constructor: it is called only once when an object is created.
      • General functions: After an object is created, it can be called multiple times.

 

5 this keyword
  • This indicates the reference of the object to which the function belongs. In other words, this indicates the reference of the Class Object.

 

  • When will this keyword be used?
    • This is used when the object that calls the function needs to be used in the function.
    • Note that this indicates the object when the method is called by that object.

 

  • Example:
package java006;public class Person {    private String name;    private int age;        public Person(String name,int age) {        this.name = name;        this.age = age;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }        public void speak() {        System.out.println(name+" "+age);    }        }
Package java006; public class PersonTest {public static void main (String [] args) {Person p = new Person ("Haha", 23); p. speak (); Person p2 = new Person ("ha", 29); p2.speak ();}}

 

6 static keywords
  • Static keyword, used to modify members (member variables and member functions ).
  • Modified members have the following features:
    • ① Load with the class.
    • ② The priority is given to the existence of objects.
    • ③ Shared by all objects.
    • ④ It can be called directly by class name.
  • Note:
    • ① Static methods can only access static members.
    • ② Static methods cannot write this or super keywords.
    • ③ The main function is static.

 

  • Main Function
    • Features:
      • ① The format is fixed.
      • ② It is recognized by JVM.
    • Detailed description:
      • The public permission must be the largest.
      • Static does not require objects, so the virtual machine is called by class name. main.
      • The void main function does not return a specific value.
      • The main function name is not a keyword, but a fixed name recognized by JVM.
      • String [] args main function parameter list, is an array type parameter, and the elements are all String type.
Package java006;/***** * Description: String [] args */public class ArgsDemo {public static void main (String [] args) {System. out. println (args); // [Ljava. lang. string; @ 1b6d3586 indicates that args has an object System. out. println (args. length); // 0 indicates that the jvm is passing new String [0];}

 

  • Static applications
    • Static variables: When all the member variables of this object are the same, this member variable can be modified using static. As long as the data is different in the object, it is the unique data of the object, which must be stored in the object and non-static. If the data is the same, the object does not need to be modified, you only need to use it, so you do not need to store it in the object and define it as static.
    • Static function: whether the function is modified using static, that is, whether the function worker accesses the special data of the object. If so, do not use static modifier. If not, you can use static modifiers. In other words, static functions can only access static variables, while common functions can access static variables or common variables.

 

  • Static code block
Package java006;/***** Note: static code blocks are loaded with classes and are executed only once. * Function: Initialize the class with a static code block. */Class StaticCode {static {System. out. println ("static code block");} public StaticCode () {System. out. println ("constructor");} public void show () {System. out. println ("show method") ;}} public class StaticCodeDemo {public static void main (String [] args) {new StaticCode (). show (); new StaticCode (). show ();}}

 

 

 

 

 

 

 

 

7 Singleton Design Mode

 

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.