Aqua-Wood Confession Studio: Java from the zero-entry simulation Headlines (ii) basic grammar

Source: Internet
Author: User

Disclaimer: All knowledge collected from the Internet, I only used to study, if there is infringement, please inform, thank you;

Java Basic syntax

Java is a purely object-oriented language, and everything is a class.

Comment string
1  Public classmain{2 3  Public Static voidPrintintIndex,object obj) {4System.out.println (String.Format ("{%d},%s", Index,obj.tostring ())); 5 }6  Public Static voidMain (string[] args) {7 8Print (1, "Hello World!!!");9 }Ten  One}

Operator variable Control flow
IfElseif() {}else{}switch(condition) {   case ' A ': Break   ;  Case  Break ;  Case  Break ; default : ...;    }
Data
1  Public Static voiddemolist () {2 3List<string> strlist =NewArraylist<string>();4   for(intI= 0;i<4;i++){5 Strlist.add (string.valueof (i));6 }7Print (1, strlist);8List<string> STRLISTB =NewArraylist<string>();9   for(intI= 0;i<4;i++){Ten Strlistb.add (string.valueof (i)); One } A Strlist.addall (STRLISTB); -Print (2, strlist);  -}

Generics support any type of list;

arraylisthashmapmap<string,string> map = new hashmap<> (); Hashtable thread-Safe set exception handling

Try () {

}catch (Exception e) {

}finally{

}

Packaging:

In the object-oriented programming approach, encapsulation (English: encapsulation) refers to a method of wrapping and hiding the implementation details of an abstract function interface.

Encapsulation can be thought of as a protective barrier against random access to code and data of that class by code defined by external classes.

To access the code and data for this class, you must pass strict interface control.

The main function of encapsulation is that we can modify our own implementation code without modifying the program fragments that call our code.

Proper encapsulation can make code easier to understand and maintain, and it also enhances code security.

Advantages of Encapsulation
    • 1. Good encapsulation can reduce coupling.

    • 2. The structure within the class can be freely modified.

    • 3. You can have more precise control over member variables.

    • 4. Hide the information and implement the details.

Steps to implement Java encapsulation

  • 1. Modify the visibility of the property to restrict access to the property (typically limited to private);
  • 1  Public class Person {2     Private String name; 3     Private int Age ; 4 }
  • 2. Provide external public method access for each value attribute, that is, create a pair of value methods for accessing the private property;
  • 1  Public classperson{2     PrivateString name;3     Private intAge ;4 ?5      Public intGetage () {6       returnAge ;7     }8 ?9      PublicString GetName () {Ten       returnname; One     } A ? -      Public voidSetage (intAge ) { -        This. Age =Age ; the     } - ? -      Public voidsetName (String name) { -        This. Name =name; +     } -}

The concept of inheritance

Inheritance is a cornerstone of Java object-oriented programming technology because it allows classes of hierarchical hierarchies to be created.

Inheritance is the child class inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the parent class's instance domain and method, or the subclass inherits the method from the parent class, so that the subclass has the same behavior as the parent class.

The inheritance format of the class

In Java, the extends keyword can be used to declare that a class is inherited from another class in the following general form:

The inheritance format of the classafter this class, there are properties and methods in the parent class, the subclass will not have duplicate code, maintainability is also improved, the code is more concise, improve the reusability of the code (reusability is mainly can be used multiple times, no longer write the same code)Inherited attributes
    • Subclasses have properties that are not private to the parent class, methods.

    • Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.

    • Subclasses can implement methods of the parent class in their own way.

    • Java inheritance is a single inheritance, but can be multiple inheritance, single inheritance is a subclass can inherit only one parent class, multiple inheritance is, for example, Class A inherits Class B, Class B inherits Class C, so the Class C is the parent class of Class B, and Class B is the parent class of Class A, which is a feature that differs from C + + inheritance.

    • Increases the coupling between classes (the disadvantage of inheritance, high coupling will cause the relationship between the code).

Inheritance Keywords

Inheritance can use the two keywords extends and implements to implement inheritance, and all classes inherit from Java.lang.Object, and when a class does not inherit two keywords, the default inherits Object (this class is in the Java.lang in the package, so no importis required) ancestor classes.

Extends keywords

In Java, the inheritance of a class is a single inheritance, that is, a subclass can only have one parent class, so extends can inherit only one class.

Implements keywords

The use of the Implements keyword can be disguised to make Java has multiple inheritance, using the scope of the class to inherit the interface, you can inherit multiple interfaces (interfaces and interfaces separated by commas).

Super and this keyword

Super Keyword: We can use the Super keyword to implement access to a parent class member to refer to the parent class of the current object.

This keyword: point to your own reference.

Final keyword

The final keyword declares that a class can define a class as not inheritable, that is, a final class, or for a decorated method that cannot be overridden by a quilt class:

Constructors

A subclass cannot inherit the constructor (constructor or constructor) of the parent class, but the constructor of the parent class has parameters, and the constructor of the parent class must be explicitly called through the Super keyword in the constructor of the child class with the appropriate argument list.

If the parent class has a parameterless constructor, it is not necessary to call the parent class constructor with super in the constructor of the subclass, and if the Super keyword is not used, the system automatically calls the parent class's parameterless constructor.

Interface: interface-based programming Java interface

Interface (English: Interface), which is an abstract type in the Java programming language, is a collection of abstract methods, and interfaces are usually declared with Interface. A class inherits the abstract method of an interface by inheriting the interface.

Interfaces are not classes, and the way they are written is similar to classes, but they are different concepts. Class describes the properties and methods of an object. The interface contains the methods to be implemented by the class.

Unless the class that implements the interface is an abstract class, the class defines all the methods in the interface.

The interface cannot be instantiated, but it can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared as an abstract class. In addition, in Java, the interface type can be used to declare a variable, they can be a null pointer, or be bound to an object implemented in this interface.

Interfaces are similar to classes:
    • An interface can have multiple methods.
    • The interface file is saved in a file that ends in. java, and the filename uses the interface name.
    • The bytecode file for the interface is saved in a file that ends in a. class.
    • The corresponding bytecode file for the interface must be in the directory structure that matches the package name.
The difference between an interface and a class:
    • An interface cannot be used to instantiate an object.
    • Interface has no constructor method.
    • All methods in an interface must be abstract methods.
    • An interface cannot contain member variables, except static and final variables.
    • Interfaces are not inherited by classes, but are implemented by classes.
    • The interface supports multiple inheritance.
Interface Features
      • Each method in the interface is implicitly abstract, and the methods in the interface are implicitly specified as public abstract(only public abstract, and other modifiers will be error-marked).
      • The interface can contain variables, but the variables in the interface are implicitly specified as public static final variables (and can only be public, with the private modifier reporting compilation errors).
      • The method in the interface is not implemented in the interface, only the class implementing the interface can implement the method in the interface.
The difference between an abstract class and an interface
    • 1. Methods in an abstract class can have a method body, which is the specific function that can implement the method, but the method in the interface is not.
    • 2. member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types.
    • 3. Interfaces cannot contain static code blocks and static methods (methods that are decorated with static), whereas abstract classes can have static code blocks and static methods.
    • 4. A class can inherit only one abstract class, while a class may implement multiple interfaces

The interface has the following characteristics:

    • Interfaces are implicitly abstract, and it is not necessary to use the abstract keyword when declaring an interface.
    • Each method in the interface is implicitly abstract, and the declaration does not require the abstract keyword.
    • The methods in the interface are public.
Refactoring polymorphism: A common method in which subclasses can override

Aqua-Wood Confession Studio: Java from the zero-entry simulation Headlines (ii) basic grammar

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.