I,
1. What is object-oriented?
Object-oriented is a programming method; Object-oriented is a way of thinking; not a programming language.
2. How should we learn object-oriented?
Understand the syntax of an object-oriented language;
Master the object-oriented way of thinking;
Familiar with object-oriented design principles;
Understanding the Object-Oriented Design Model
3. What is an object-oriented thinking method?
First, determine who will do it, and then determine how to do it;
First, consider the whole, and then consider the local;
First, consider abstraction, and second, consider specifics.
II,
1. Create a class
Class Name
{
Attribute; (also called a member variable, mainly used to describe the status of the Class)
Method; (also called member method, used to describe the behavior of a class)
}
2. Object Creation Method
Format: Class Name object name = new Class Name ();
Example: Dog dog = new Dog ();
Dog dog = creates a reference for a Dog and stores it in the stack memory.
= New Dog (); creates a Dog object and stores it in heap memory.
3. Object and object references
Objects are stored in heap memory, and objects are referenced in stack memory.
III,
Object usage
Call variables and functions using objects:
Object. Variable
Object. function ()
How to create multiple objects
Dog d1 = new Dog ();
Dog d2 = new Dog ();
How to create and use anonymous objects
You do not need to define the reference name of the object. And directly call the method of this object. Such an object is called an anonymous object, for example:
New Dog (). jump ();
New Dog (). jump ();
It disappears after use, and the second anonymous object is newly created.
IV,
Function Overloading
Criteria: Two or more functions are in the same class; functions have the same name; function parameter lists are different.
Class A {void funA () {System. out. print ("funA function without Parameters" + "\ n");} void funA (int a) {System. out. print ("funA function with an integer parameter ");}}
The two funA functions are overloaded.
class TextA{ public static void main(String args[]){ A a = new A(); a.funA(); a.funA(10); }}
Therefore, all funA functions can be called.
Role of Constructors
The constructor is called after new.
Syntax:
The constructor name must be the same as the class name;
The constructor does not define the return value;
Use new to call the constructor;
If no constructor exists in the class, the compiler automatically adds a constructor whose parameter is null and the method body is null. If yes, it will not be added.
Class A {A () {// constructor }}
Example:
Class Person {Person () {} Person (String n, int a) {// The self-defined constructor overload name = n; age = a;} String name; int age ;}
Class TextPerson {public static void main (String args []) {Person person1 = new Person ("zhangsan", 10); Person person2 = new Person ("lisi", 20 ); system. out. print ("person1 name:" + person1.name + ", age:" + person1.age + "\ n"); System. out. print ("person2 name:" + person2.name + ", age:" + person2.age );}}