[52PJ] Java Object-oriented note (turn from 52 1510988116)

Source: Internet
Author: User


Object-oriented concepts
    • Object-oriented three major features: encapsulation, inheritance, polymorphism
    • Object-oriented programming (Oop,object oriented programing) is relative to process-oriented programming, the previously written code is basically purely process-oriented programming , when the project is complex, then the implementation of pure process-oriented code is very complex, Object-oriented can simplify the structure and organization relationship of code . object-oriented is not an alternative to process-oriented, macroscopic is object-oriented, microscopic is still a process oriented .
    • Classes and Objects (object): People, Leehom Wang

definition of Class
    • Simplest class: Class person{}

Class person{
private int age;//member variable
private String name;
public void Setage (int.)
{
This.age=age;
}
public void SetName (String name)
{
This.name=name;
}
public void SayHello ()
{
System.out.println ("Hello everyone, I am" +name+ "I Am" +age+ "year old");
}
}
    • Only one public class can be defined in a Java file, and the file name must be the same as the public category

instantiation of an object

Person Yzk=new person ();
Yzk.setname ("Yang Zhengko");
Yzk.setage (18);
Yzk.sayhello ();

Person Lzy=new person ();
Lzy.setname ("Lin");
Lzy.setage (80);
Lzy.sayhello ();
Memory allocation for two objects: copy two copies according to the template.
Yzk.sayhello ();
each new object is a separate instance, and the member variables between the two objects are two separate parts. New called Class object or instance (Instance)

class object is a reference pass
Person Weiren=yzk;
Weiren.setage (18);
Weiren.sayhello ();
Yzk.sayhello ();
Explanation: "Person weiren=yzk;"Weiren points to the object that the Yzk is currently pointing to;

member variables and local variables
public void Setage (int.)
{
This.age=age;
}
1.Local variables must be initialized,default initialization of member variable declarations,The base numeric type is initialized to null by default to non-basic types such as int,string. Why involves stack memory and heap memory, which is later said.
2.when a member variable and a local variable (a function argument can also be considered a local variable), it is considered a local variable, so in order to avoid confusion, it is recommendedwhen accessing a member variable, add "This", which represents the current object.
3.new Person (). SayHello ();Create aAnonymous Objects, and then call.

Private/public
    • We can declare the age member variable as public, or declare setage as private, so that only private members can be called Inside the class (and then write a method to call private Setage)
    • Conclusion: Publicmembers can be accessed internally or externally by the class andprivate members can only be accessed within the class, thus protecting internal members (Member, including fields field/variables, methods) that do not want to be called outside the outside world.
    • What harm does it do by setting the age of the public, without assigning a value through Setage?
    • Field/member variable (Member Variable) is generally declared as private

Constructors (Constructor)
    • A constructor is a special function that creates a class object and initializes the class before the creation is complete . If you define a class without declaring a constructor, by default a parameterless constructor is given, and if any of the constructors are defined, no default parameterless constructor is provided.
    • Construction method format and features:

the method name must match the class name
no return value type
3.Constructors can overload, person (String name,int age)
4, with the constructor initialization and new after the set*** initialization by the difference: semantics, some objects are born to have some member variables are assigned, or freak

Static statics
    • Some scenarios require multiple instances of a class to share a member variable; sometimes you define "constants", but Java does not provide constants; Sometimes you want to define something that is not associated with a specific object and does not require new to invoke the method.
    • The static method can be called directly from the class name without the need for new.
    • Static variables can be called directly from the class name without the need for new.
    • The This keyword cannot be used in the static method because static is independent of the object and is not unique to anyone.
    • Only static members can be accessed in a static member, and non-static members cannot be accessed directly. Non-static members can access static members.


single-case mode
    • Some classes can have only one object in the system (*, Resource Manager, cache manager, etc.), then use the "singleton Mode" (singleton). There are many ways to implement the singleton mode, first introduce the simplest and most practical "a Hungry Man"
    • The constructor is declared private so that it avoids outside access
    • Defines an object instance of private final static
    • Defines a public static getinstance method that returns a unique instance.

Static code block for class
Class MyTest
{
Static
{
System.out.println ("Zi Static code block")
}
Public MyTest ()
{
System.out.println ("Zi construction Method")
}
}
MyTest t1=new MyTest ();
MyTest t2=new MyTest ();
static code blocks are executed once when the class is first used and executed before the constructor executes, as long as the class is used, even if no new object (such as just declaring a variable) is executed and executed only once, typically to initialize the class.
Packages (Package)
    • Use the file system's folder to explain the problem of avoiding duplicate file names.
    • Package Syntax:

Defined at the top of the page through the package;
The save path on disk is consistent with the package, and can be single-stage, multi-level
3.Package Name "Unspoken rules"
Package Name Lowercase
"The company domain name is written back." Product name. Module Name: Com.rupeng.crm.user, Com.sum.media.sound
4. The classes in the current package do not need to be referenced; classes in other packages:
ImportCom.rupeng.crm.user.* (not recommended, too many references at a time, easy to conflict)
Import Com.rupeng.crm.user.Person; (recommended)
When using Java.sql.Array, do not import, applicable to the use of multiple names at the same time the class;
5, the contents of the Java.lang without manual import
Inheritance (Inherit)
    • A class in Java can "inherit from another class, if a inherits from B", then A is called a subclass of B, B is called a parent class (base class). Subclasses inherit all non-private members from the parent class. Subclasses can also have subclasses.
    • A class in Java can have only one parent class, and if no parent class is specified, the Java built-in object is the parent class

Class Fulei
{
private void Method1 ()
{
}
private void Method2 ()
{
}
}
Class Zilei extends Fulei
{
private void Method3 ()
{
}
private void Method4 ()
{
}
}
Zilei zi1=new Zilei ();
Zi1.nethod1 ();
ZI1.NETHOD2 ();
ZI1.NETHOD4 ();
Zi1.tostring ();
sequence of constructor calls in inheritance
    • The constructor method of a subclass accesses the non-parametric constructor of the parent class by default: There is a line in the constructor method in the subclass: Super ()

Class Fu
{
Public Fu ()
{
System.out.println ("Fu");
}
}
Class Zi extends Fu
{
Public Zi ()
{
Super ();//Whether the call is displayed or not, the console will output fu
System.out.println ("Zi")
}
}
Zi z=new Zi ();
The constructor of the parent class is executed before the parent class is initialized, and the subclass's
private/public/protected
    • Private members are not able to access the quilt, the outside world, so as to ensure the security of private members
    • Protected members can only be accessed by themselves and by subclasses (directly or indirectly) and by siblings (in the same package), not by "alien people"


Enumeration
    • Some data are open range, such as int, float, String. Some data optional value is a limited range of values, such as constellation, month, direction, if you use 1/2/3/4 to represent the cardinal, that in case set up 8 what to do?
    • An enumeration is a special type that defines the range of values to determine (the optional value is generally uppercase). (syntax after JDK5)

Enum Dir
{
East,west,north,south;
}
Use Dir d=dir.east;
3, (*)methods can also be defined in enum types: Write ";" For example: Getintvalue (the represented integer), parse (resolves the integer to an enumeration object) after all enumeration definitions have been completed. Common usage: Define a definite value for the enumeration. constructor to be defined as private (why)

polymorphic
    • Object-oriented three major features: encapsulation, inheritance, polymorphism. Polymorphism is one of the most powerful features of object-oriented, it is also the most difficult one, and the design pattern is the embodiment of polymorphism. Large-scale project architectures are also heavily applied polymorphic.
    • The same method defined in the subclass as in the parent class is called overriding (override) or overwrite.

Class Diqiuren
{
pubic void speak () {System.out.println ("I Am the earth Man");}
}
Class Chinese extends Diqiuren
{
@Override
public void Speak () {System.out.println ("I am Chinese");}
public void Bainian () {System.out.println (good New Year);}
}
Diqiuren dqr1=new Diqiuren ();
Dqr1.speak ();
Chinese zgr1=new Chinese ();
Zgr1.speak ();
What is the result of the following execution?
Chinese zgr2=new diqiuren ();//The Earth people are not necessarily Chinese
Diqiuren dqr2=new Chinese ();
Dqr2.speak ();
Diqiuren Dqr2=zgr1;
dqr2.speak ();
    • The parent class variable can point to a subclass object , and in memory it is also a subclass object, with and only one object. The type of variable is not related, and the way in which it is executed depends primarily on what type of object is in memory .
    • The variable type is "what to think of the object", Diqiuren dqr2=new Chinese (), the Chinese object is regarded as "earth Man", because "earth man" does not necessarily have Bainian method. Therefore, the Dqr2.bainian () compilation fails.

Final
    • if some methods in the parent class do not want the quilt class to override, such as Kill, the tag is final . Benefit: security, which guarantees the execution of the behavior defined by the parent class (must be "killed"), and the high efficiency, avoids querying the virtual method table (*).
    • If some classes do not want to be inherited, the class can also be declared final.
    • the benefits of the final class are primarily security (there will be no imposter "black Sheep"). The string class is final.

Abstract class
    • Review: Abstract classes are marked abstract and abstract classes cannot be new.
    • Earth man's sayhello output "I am the earth man" is obviously unreasonable, because cannot determine how to say, that is, the Earth people do not know how to SayHello, only specific to the Chinese, Japanese, Americans know how to SayHello
    • The SayHello method of the Earth's human body is removed, and the method increases the abstract modification, and the class is also modified as abstract:

Abstract class Diqiuren
{
public abstract void speak ();
}
There is no method body for abstract methods;once the abstract method is defined in the class, the class must be decorated as abstract; the abstract class cannot be instantiated (new)
Abstract class sub-class
    • Abstract class abstraction methods are mainly used to restrict subclasses "must implement these abstract methods"; Subclasses can also not be implemented, then subclasses are abstract classes, subclasses of subclasses ..... To achieve
    • Eclipse can overwrite/implement the parent method through alt+/or the main menu "source"--"Overwrite/implement method" implementation.

Interface
    • An interface is a type that declares "capabilities," and does not provide implementations that do not provide implementation methods, even {}.
    • Interfaces generally end with "able" (the words "ability" in English generally end with able).
    • The interface cannot be instantiated and can only be "implemented" by the class (implements)

public  interface  speakable
{
    public void speak ();
}
Public class teachercang  implements  speakable
{

}
      4, You can use either an interface type variable or a class type variable to invoke the meaning of the Speak interface is to define "what to do" and the class definition "what to do"
Interface Drill-down
    • Review: The interface is declared with interface, the method defined in the interface cannot provide the implementation, and the {} Null method does not work. The interface cannot be new and can only be implemented by the class (implements).
    • interfaces generally end with "Able"(the words "ability" in English generally end with able ). Ctrl+shift+t look at the system. The interface name to verify.
    • only static final member variables can be declared in an interface, and ordinary member variables cannot be declared. because static final can be used by outsiders, it is meaningless to declare ordinary member variables in an interface that does not implement code.
    • multiple methods can be defined in an interface. You can also not define any method (* Identity interface).
    • Interfaces are simply "capabilities" not "implementations", so you cannot and do not have to define constructors.
    • A class can have only one parent, and a class may implement multiple interfaces.
    • Interfaces can "implement" other interfaces and also use implements, not extends, and therefore cannot be called "inherited interfaces."
    • The polymorphism, type conversion of the interface.




[52PJ] Java Object-oriented note (turn from 52 1510988116)

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.