Java BASICS (5): java Basics

Source: Internet
Author: User

Java BASICS (5): java Basics
1. Object-oriented

Object-oriented: a programming idea

1, Class and Object

ClassIt refers to describing a class of things, or as a classification. It can regard classes as templates for constructing objects.

  ObjectIt refers to a specific individual (also called an instance-instance ). Create an object using new. If no object exists, a new object is created.

1. Design

Syntax:

1 class name {2 member variable (field) 3 method (specific functional behavior) 4}

Writing class specifications:

① Class names should start with uppercase letters;

② The class name must be meaningful;

③ Annotations must be written;

1 public class Car {2 String name; // Field 3 Integer price; 4 5 void run () {// method, specific behavior 6 System. out. println ("run"); 7} 8}
2. Constructor

  Creating an object is essentially calling the constructor of a class.

1 public class Car {2 String name; // Field 3 Integer price; 4 5 void run () {// method, specific behavior 6 System. out. println ("run"); 7} 8 9 // constructor, no parameter constructor 10 public Car () {11} 12}

Features:

1. Each class has at least one constructor. If no constructor is displayed (displayed), an implicit constructor without parameters exists. If a class contains a constructor, then the Implicit One does not exist.

2. the constructor name is the same as the class name (including the case and case ).

3. No return value type. No data needs to be returned within the constructor.

4. Other and common method types can be modified (public), can have the form parameter list, and can have the method body.

5. assign a value to the field of the object while creating the object. This parameter can be used as a constructor.

6. the constructor is always called along with the new operation.

1 public class Car {2 String name; // Field 3 Integer price; 4 5 void run () {// method, specific behavior 6 System. out. println ("run"); 7} 8 9 // constructor 10 public Car () {11} 12 13 // construction method with parameters 14 public Car (String name, Integer price) {// assign 15 this to the object field while creating the object. name = name; 16 this. price = price; 17} 18}
3. Encapsulation

 In object-oriented programming, encapsulation refers to a method that encapsulates and hides the Implementation Details of abstract interfaces (data hiding ).

Embodiment in Java

1. privatize fields (instance domains) in the class;

2. Provide a set of getter and setter methods for each field (standard method );

3. Create an object in the test class and call the getter setter method to assign values to the field;

Advantages:

Appropriate encapsulation makes the code easier to understand and maintain, and enhances the code security. Reduce coupling. The internal structure of the class can be freely modified. You can control member variables more accurately. Hide information and implement details.

1 public class Student {2 private String name; // privatized Field 3 private Integer age; 4 5 // getter and setter methods 6 public String getName () {7 return name; 8} 9 10 public void setName (String name) {11 this. name = name; 12} 13 14 public Integer getAge () {15 return age; 16} 17 18 public void setAge (Integer age) {19 this. age = age; 20} 21}
This introduction:

  When the parameter names in the setter method are the same, select the proximity principle and use this.

4. Inheritance1. Specific implementation

This improves code maintainability and reusability, and makes the code more concise.

The syntax format inherited from the Java class:

1 class A{ }2 3 class B extends A{ }

A is the parent class of B, and B is the Child class of. Sub-classes can inherit from the parent class.

1/** 2 * parent class 3 */4 public class Animal {5 6 public void move () {7 System. out. println ("move"); 8} 9 10}

Subclass. Non-private fields and methods can be inherited. Constructor cannot be inherited.

1/** 2 * subclass 3 */4 public class Cat extends Animal {5 6 @ Override 7 public void move () {8 super. move (); // super Keyword: The super keyword is used to access the parent class members and reference the parent class of the current object. 9} 10}

Java only supports single inheritance, that is, a class has only one parent class, but supports multiple inheritance (Multi-Level inheritance). That is, a class can have subclasses, and subclasses can also be subclasses... son, son, son, sun, endless...

2. Override)

Method rewriting requirements:

1. The method signature of the subclass and the parent class (method name + method parameter list) are consistent.

2. The sub-class cannot have lower access permissions than the parent class.

3. It is best to add @ Override to let the compiler check whether the rewrite is correct.

4. Private and static methods cannot be overwritten.

5. the return value type of the subclass can be the same as that of the parent class or the return type of the parent class.

1/** 2 * subclass 3 */4 public class Cat extends Animal {5 @ Override 6 public void move () {7 System. out. println ("move on"); // override the parent class method 8} 9 10}
5. Interfaces

 Interfaces are usually declared using interfaces. A class inherits the abstract methods of interfaces by inheriting interfaces.

Statement Syntax:

Interface name {// which Members can be contained inside the interface? reference class fields are all global constants (public static final modifier). All the methods are abstract methods (public abstract is modified by default) no constructor !}

Generally, the interface name is preceded by an uppercase I.

1 public interface IAnimal {2     3     void run();4     5     void eat();6 }

The implements keyword can be used to make java have the multi-inheritance feature in disguise. When the scope is the class inheritance interface, multiple interfaces can be inherited at the same time (interfaces and interfaces are separated by commas ).

Note:

1. All abstract methods in the interface must be overwritten.

2. Otherwise, the implementation class is also an abstract class.

3. A class can implement multiple interfaces, but the abstract methods in all interfaces must be overwritten.

 1 public class AnimalImpl implements IAnimal { 2     @Override 3     public void run() { 4         System.out.println("run"); 5     } 6  7     @Override 8     public void eat() { 9         System.out.println("eat");10     }11 }
6. Polymorphism

Polymorphism is the ability of a single behavior to have multiple different forms or forms. That is, the same event will produce different results on different objects.

Advantages of polymorphism:

1. Eliminate coupling between types

2. Alternative

3. scalability

4. Interface

5. Flexibility

6. Simplification

Three necessary conditions for Polymorphism

1. Inheritance

2. Rewrite

3. parent class references to subclass objects

1/** 2 * parent class 3 */4 public class Animal {5 public void eat () {6 System. out. println ("eat"); 7} 8} 9 10/** 11 * subclass 12 */13 class Cat extends Animal {14 public void eat () {15 System. out. println ("cat fish"); 16} 17 18 public void work () {19 System. out. println ("cat and mouse"); 20} 21} 22 23 class Dog extends Animal {24 public void eat () {25 System. out. println ("Dog Bone"); 26} 27 public void work () {28 System. out. println ("dog watching"); 29} 30} 31 32 class Person extends Animal {33 public void eat () {34 System. out. println ("people eat"); 35} 36 public void work () {37 System. out. println ("Working Hard"); 38} 39}

Test polymorphism:

1 public class Test {2 public static void main (String [] args) {3 Animal a = new Cat (); // converts 4. eat (); // call Cat's eat: Check whether the method exists in the parent class. If not, the compilation is incorrect. If yes, call the method with the same name as the subclass. 5 Cat c = (Cat) a; // downward transformation, forced conversion 6 c. work (); // call Cat's work 7 8 Animal B = new Dog (); 9 B. eat (); 10 Dog d = (Dog) B; 11 d. work (); 12 13 Animal person = new Person (); 14 person. eat (); 15 Person p = (Person) person; 16 p. work (); 17 18/* output: 19 cats eat fish 20 Cats catch mice 21 dogs eat bones 22 dogs watch 23 people eat 24 people work hard */25} 26}
Implementation of Polymorphism

1. Rewrite

2. Interfaces

3. abstract classes and abstract methods

 

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.