Java from getting started to mastering 04-classes, objects

Source: Internet
Author: User
Tags class definition modifiers

The Java language is object-oriented, and the development of computer language evolves towards the way of thinking.

The difference and connection between the class and the counterpart box
1, the class is abstract, the concept, represents a class of things, such as human, cat ...
2, the object is concrete, actual, represents a thing, such as Zhang San, John Doe ...
3, the class is the object template, the object is an individual, instance of the class;

A definition template for a comprehensive class:

 Package package name; class extends Implements Interface Name {    member variable;    construction method;    member method;}

The parameters of a member method of a class can be multiple, and the method can have no return value;

The construction method is a special method of the class, its main function is to complete the initialization of the new object, it has the following characteristics:
1, the construction method name and the class name are the same;
2, the construction method has no return value;
3, the main role is to complete the initialization of new objects;
4, when creating a new object of a class, the system automatically calls the construction method of the class to complete the initialization of the new object;
5, a class can define a number of construction methods;
6, each class has a default construction method, when the construction method is defined, will replace the default construction method;

1 classperson{2     intAge ;3 String name;4     //Construction Method5      PublicPerson (intage,string name) {6          This. age=Age ;7          This. name=name;8     }9}

This can no longer be used externally by a class definition and can only be used within a method defined by the class.

A class variable is a variable that is shared by all objects of that class, and when any object of that class accesses it, the value that is taken is the same, and the same variable is modified when any object of that class modifies it.

Class variable definition method: Access modifier static data type variable name;
Access methods for class variables: class name. class variable name or object name. class variable name;

 public  class   child { int      age;    String name;    static  int  total = 0; //     public  Child (int   this . Age = this . Name = name;  public  void   Joingame () {total  ++; }}

The class method belongs to all object instances, such as: access modifiers static data return type method name () {};
Non-static variables (class variables) cannot be accessed in a class method;
Class methods can be accessed directly through the class name. class method name;
The class method belongs to the public method related to the class;

 Public classStudent {intAge ;    String name; intfee; Static intTotalfee;  PublicStudent (intAge, String name,intfee) {         This. Age =Age ;  This. Name =name; Totalfee+=fee; }    //return total tuition, this is a class method that saves memory overhead, in principle through the "class name. Class method" to access     Public Static intGettotalfee () {returnTotalfee; }}

Four features of Java object-oriented programming: abstraction, encapsulation, inheritance, polymorphism;

Abstract: It is to extract the common attribute and behavior of a kind of things, to form a house model, this method of research problem is called abstraction;
Encapsulation: It is the abstraction of data and the operation of the data is encapsulated together, the data is protected inside, the other parts of the program only through the authorized operation (member method), in order to manipulate the data;
Inheritance: Can solve code reuse, let our programming closer to people's thinking, when multiple classes have the same attributes (variables) and methods, you can abstract from these classes of the parent class, in the parent class to define the same properties and methods, all subclasses do not need to redefine these properties and methods, Simply declare the inherited parent class through the extends statement, the class subclass extends the parent class, and the public, protected, and default of the parent class can inherit;
Polymorphic: Refers to the multiple states of a reference under different circumstances, or it can be understood as: polymorphism refers to a method that is implemented in a different subclass by pointing to a pointer to the parent class.

Inheritance considerations:
1, subclasses can inherit at most one parent class (refer to direct inheritance);
2. All Java classes are subclasses of the object class;
3. The package in the JDK contains classes, interfaces, exceptions, enumerations, annotations, and errors;

Access Control modifiers
1, the public level: with public, external;
2, Protected level: With protected decoration, sub-class and the class in the same package is exposed;
3, the default level: No modifier, to the same package of classes exposed;
4, Private level: with private decoration, only the class itself can be accessed, not public;

The package's role:
1. Distinguish between classes of the same name;
2, when a lot of classes, can be very good management class;
3. Control the scope of access;

Method Overloading (Overload): is the same function in the class of multiple implementations, in which way, depending on the parameters given by the caller;
1, the method name is the same;
2, the method's parameter type, the number, the order at least one is different;
3, the method return type can be different;

Method Override: The subclass has a method that is the same as the name, return type, and parameters of a method of a parent class, so we say that this method of the subclass overrides that method of the parent class.
1. The return type, parameter, and method name of the method of the subclass are exactly the same as the return type, parameter, and method name of the parent class method;
2, the subclass method can not reduce the access rights of the parent class method;

 Public classDemo { Public Static voidMain (String args[]) {Master Master=NewMaster (); Master.feed (NewDog (),NewBone ()); Master.feed (NewCat (),NewFish ()); }}classAnimal {String name; intAge ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     Public voidcry () {System.out.println ("Don't know what to call it."); }     Public voideat () {System.out.println ("I don't know what to eat."); }}classCatextendsAnimal { Public voidcry () {System.out.println ("Meow, Meow."); }     Public voideat () {System.out.println ("Cats like to eat fish."); }}classDogextendsAnimal { Public voidcry () {System.out.println ("Bark."); }     Public voideat () {System.out.println ("Dogs like to eat bones."); }}classFood {String name;  Public voidShowName () {}}classFishextendsFood { Public voidShowName () {System.out.println (Fish); }}classBoneextendsFood { Public voidShowName () {System.out.println (Bones); }}classMaster {//using polymorphism, the method can be used with a     Public voidFeed (Animal an, food f) {an.eat ();    F.showname (); }}

Java from getting started to mastering 04-classes, objects

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.