5. Object-oriented in Java (1) notes, java object-oriented

Source: Internet
Author: User
Tags float double

5. Object-oriented in Java (1) notes, java object-oriented

1. Object-oriented VS process-oriented

1/* 2 * understanding 1: people open doors 3 * process-oriented: people open doors 4 * object-oriented: 5 * People {6 * open () {7 * doors. Open (); 8 *} 9 *} 10 * door {11 * open () {12 *} 13 *} 14*15 * understanding 2: people put elephants in the refrigerator 16 * oriented process: 17*1. open the refrigerator; 18*2. Put the elephants in; 19*3. Close the refrigerator door; 20 * object-oriented: 21 * Persons {22 * Open (refrigerator) {refrigerator. open ()} 23 * operation (ELEPHANT) {elephant. Enter (refrigerator)} 24 * close (refrigerator) {refrigerator. Close ()} 25 *} 26 * elephant {27 * enter (refrigerator) {} 28 *} 29 * refrigerator {30 * open () {} 31 * close () {} 32 *} 33 */

2. object-oriented programming focuses on class design!

2.1. No matter how large a project or project is, there must be one class.

2.2. the class is abstract, like the drawing of a car. A specific car is made based on drawings, and is actually a class instantiation.

1/* 2*1. object-oriented programming focuses on Class Design 3*2. design classes are actually design class members 4*3. Basic class members, attribute (member variable or Field) & Method (Method) 5 */6 public class TestPerson {7 public static void main (String [] args) {8 Person p1 = new Person (); 9 p1.info (); 10 11 p1.name = "zhongchao"; 12 p1.age = 21; 13 p1.sex = true; 14 p1.info (); 15 16 p1.setName ("Zhang San"); // p1.name = "Zhang San" 17 p1.info (); 18 19 Person p2 = p1; 20 System. out. println ("P1:" + p1); 21 System. out. println ("P2: "+ P2); 22 p2.info (); 23 24 p2 = new Person (); 25 System. out. println ("P2:" + p2); 26 p2.info (); 27 // instantiate an object of the lifecycle class through this object. the nextXxx method is called to complete the corresponding functions. 28 s = new release (System. in); 29 int I = s. nextInt (); 30 31 Person p3 = new Person (); 32 p3.info (); 33} 34} 35 // class: abstract. 36 class Person {37 // 1. property 38 String name; 39 int age = 10; 40 boolean sex; 41 42 // 2. method 43 public void eat () {44 System. out. println ("people eat"); 45} 46 public void sleep () {47 System. out. println ("sleeping"); 48} 49 50 public String getName () {51 return name; 52} 53 public void setName (String n) {54 name = n; 55} 56 57 public void info () {58 // eat (); 59 // sleep (); 60 System. out. println ("name:" + name + "age:" + age + "sex:" + sex); 61} 62}

3. Idea of completing a project (or function)

3.1. Whether the object of the class corresponding to the function to be completed exists;

3.2 If yes, you can directly call the attributes or methods in the corresponding class through the object;

3.3 If the object does not exist, you need to create a class object. Even if the classes do not exist, design classes are required;

4. Three main lines of object-oriented programming:

1. Composition of classes and classes: internal class of the attribute method constructor code block

2. Features of object-oriented programming: encapsulation inheritance polymorphism (abstraction)

3. Other keywords: this super package import static final abstract interface...

Class:

1. Focus on Class Design

2. Composition of the class:

1. attributes (member variable, Field)

2. Method (member Method, function, Method)

3. attributes:

4. Method:

1/* 2*1. Implementation of Object-oriented Thinking Principle 1: 3*1. design and design class members (member variables & Methods) 4*2. create a Class Object (also called class instantiation) through a class 5*3. Use an object. Attribute "or" object. Method "to call, complete the corresponding function 6*7*2. Create multiple objects, each having their own set of class attributes, when modifying the attributes of one of the objects. 8 * does not affect the attribute values of other objects. 9*10*3. class attributes (member variables) 11 * member variables VS local variables 12 * similarities: 1. Follow the format of the variable Declaration; data type variable name = initialization value; 13*2. Scope: 14 * differences: 1. Declaration location: member variables: Life in the class, out of the method; 15 * local variables: declaration in the method, the form parameter of the method, 16*2 in the code block, the modifier of the member variable has four: public (public) private (private) protected 17 * local variables do not have modifiers by default: the modifier is the same as the modifier of the method in which it is located; 18*3. initialization value: there must be an initialization value; 19 * member variable: if the value is not displayed during the declaration, different data types will have different default initialization values; 20 * byte short int long = 0; 21 * float double = 0. 0; 22 * char = space; 23 * boolean = false; 24 * reference similar variables = null; 25 * local variables: assign values to be displayed (local variables do not have default initialization values); 26*4. They are stored in different locations in the memory, and member variables are in the heap space, local variables are in the stack space; 27 * Summary: Classification of variables: 1. According to different data types: Basic Data Types (8 types) & reference data type 28*2. Different declared locations: member variables & local variables 29*30*4. Class methods: provides some function implementation 31*1. instance public void eat () {method body} 32 * public String getName () {} 33 * public void setName (String n) {} 34*2. Format: Permission modifier return value type (void: no return value/specific return value) method name (shape parameter) {} 35*3. About Return Value Type void: Table name this method does not require a return value 36 * with a return value: at the end of the method, there must be a variable 37 * corresponding to the return + return value type. Memory: void and return cannot appear in a method at the same time. 38*4. Other methods and attributes of this class can be called in the method, but other methods cannot be defined in the method; 39*40 * ClassRoom Car... 41 */42 public class Zoo {43 public static void main (String [] args) {44 // declaration of basic data types: data type variable name = initialization value; 45 int I = 10; 46 // 1. class instantiation: The following a1 is a real object 47 Animal a1 = new Animal (); 48 // int [] arr = new int [10]; 49 // call the property 50 a1.name = "Hua"; 51 a1.age = 3; 52 System. out. println ("name:" + a1.name + "\ t" + "age:" + a1.age); 53 // pass Object call method 54 a1.eat (); 55 a1.sleep (); 56 57 // create an object 58 Animal a2 = new Animal (); 59 System. out. println ("name:" + a2.name + "\ t" + "age:" + a2.age); 60 a2.name = "Xiaohua"; 61 System. out. println ("name:" + a1.name + "\ t" + "age:" + a1.age); 62 System. out. println ("name:" + a2.name + "\ t" + "age:" + a2.age); 63 // a3 is the same as A1, a3 does not mean that a1 and a3 share an object entity 64 Animal a3 = a1; 65 System. out. println ("name:" + A3.name + "\ t" + "age:" + a3.age); 66 a3.name = "Winnie the bear"; 67 System. out. println ("name:" + a1.name + "\ t" + "age:" + a1.age); 68 69 System. out. println (a2.getName (); 70 System. out. println (a2.desc (); 71} 72} 73 74 class Animal {75 // 1. property 76 String name; 77 int age; 78 // 2. method 79 public void eat () {80 System. out. println ("animal food"); 81} 82 public void sleep () {83 System. out. println ("Animal sleep"); 84 // return; 85} 86 public String getName () {87 return name; 88} 89 public int getAge () {90 return age; 91 // The statement 92 // System cannot be declared afterwards. out. println ("Hello"); 93} 94 // when this method is called through an object, the return value of the method is provided to the caller of the method, that is, the current object. 95 public String desc () {96 if (age> 2) {97 return ""; 98} else {99 return "still watching cartoons "; 100} 101} 102 public void setName (String n) {103 name = n; 104} 105 public void addAge () {106 int I = 2; 107 age + = I; 108} 109 public void info () {110 // other methods of this class can be called in the method, but the new del method 111 eat () cannot be defined in the method (); 112 sleep (); 113 // public void breathJ () {114 // System. out. println ("breathing"); 115 //} 116} 117 // System. out. println ("Hell O! "); 118}

5. Rule 1 of object-oriented programming:

1. Design and create the class and its components;

2. instantiate the class object;

3. Complete a function in the form of "object. Attribute" or "object. Method"

6. class initialization memory parsing:

6.1 structure of memory Division:

Stack: local variable, Object Reference name, array reference name

Heap: A new "thing" (such as an object and an array object), including a member variable.

Method Area: Contains string constants

Static Field: variable declared as static

6.2. On the basis of understanding, you should learn how to basically create class objects to run in memory.

7. Method overload ):

Requirements: 1. In the same class;
* 2. The method name must be the same;
* 3. The parameter list of the method is different. 1. The number of parameters is different;
* 2. Different parameter types;
* 4. Method Overloading is irrelevant to the return value type of the method;

1/* 2 * method overload 3 * requirements: 1. In the same class; 4*2. The method name must be the same; 5*3. Different parameter lists of methods; 1. different numbers of parameters; 6*2. Different parameter types; 7*4. There is no relationship between method overloading and return value types of methods; 8 */9 public class TestOverLoad {10 11} 12 class OverLoad {13 // define two int variables and 14 public int getSum (int I, int j) {15 return I + j; 16} 17 // defines the sum of the three int variables and 18 public int getSum (int I, int j, int k) {19 return I + j + k; 20} 21 // cannot be combined with several other methods to form a overload 22 // public int getSum1 (int I, int j, int k) {23 // return I + j + k; 24 //} 25 // public void getSum (int I, int j, int k) {26 // System. out. println (I + j + k); 27 //} 28 // defines the sum of two double data types and 29 public double getSum (double d1, double d2) {30 return d1 + d2; 31} 32 // defines three double data types and 33 public void getSum (double d1, double d2, double d3) {34 System. out. println (d1 + d2 + d3); 35} 36 // The following two methods also constitute an overloaded 37 public void method1 (int I, String str) {38 39} 40 public void method1 (String str1, int j) {41 42} 43}

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.