20165318 2017-2018-2 "Java Programming" Fourth week study summary idea installation
According to Lou teacher Intellj idea Simple tutorial, I downloaded idea but because there is no registered school mailbox, I can not apply for free use, I found on the internet IntelliJ idea 2017 registration code free activation method successfully installed and run ideas.
There can be only one parent class per subclass, and a parent class may have more than one child class. You can use keywords extends
to define a subclass of a class:
Class extends
is the object class by default when no keyword is added, and the object class is the ancestor class of any class (except itself).
- Inheritance of subclasses
- When subclasses and parent classes are in the same package, subclasses inherit
private
member variables and methods that are not in the parent class, and their access rights remain unchanged.
- When a subclass is not in the same package as the parent class, the subclass does not inherit and
private
the member variables and methods of the friendly access permissions are inherited and only protected
public
.
If a class A wants to invoke its variable or method by creating an object from a subclass C of another class B: ① to invoke the new definition within subclass C instead of inheriting, as long as C and a are the same package; ② to invoke C inheritance, as long as C inherits the ancestor class with a same package.
A subclass can manipulate an inherited variable by inheriting the method.
instanceof Operator: When the left operand is an object created by the right class or subclass, the result of the operation is true
, otherwise false
.
a method inherited by a subclass can only manipulate member variables that are inherited and hidden by subclasses . A subclass of a newly defined method can manipulate subclass inheritance and new declared member variables of subclasses, but cannot manipulate member variables that are hidden by subclasses.
Method overrides: Requires that the type of the new method be consistent with the corresponding method of the parent class or the type of the method of the parent class , and that the name of the method, the number of arguments, the type of the parameter, and the method of the father are identical. For example:
class A {Object get() { return null;}}class B extends A {Integer get() { //Integer是object的子类 return new Integer(100);//返回一个Integer对象}}
The overriding method can manipulate inherited variables, methods, or manipulate the newly declared member variables, methods of the subclass, but cannot manipulate the member variables hidden by the quilt class . If a subclass wants to use a method or member variable that is hidden, use it super
. Note that overriding the method does not allow you to reduce access to the method, but you can increase access rights .
When using overrides, be careful to distinguish between overrides, overloads, and syntax errors.
float computer(float x,float y)//父类中的方法。float computer(float x,float y)//子类中的方法,是重写。double computer(float x,float y)//子类中的方法,语法错误,既不是重写,也不是v重载float computer(float x,float y,double z)//子类中的方法,是重载。
How to use Super : Use commands super.[成员变量]
and super.[方法]
to separate the hidden variables and methods.
Subclasses do not inherit the construction method of the parent class. Super must be the first statement of a subclass construction method . If the Super keyword is not written out in the subclass, it is super()
called by default.
When you define multiple construction methods in a parent class, you should include a constructor without parameters, because Java does not provide a default construction method (without the constructor of the parameter), which causes an error when Super is omitted.
- final keyword: The final keyword can modify local variables in classes, member variables, and methods.
- Modifier class: A class that is decorated cannot have subclasses.
- Methods to decorate the parent class: Subclasses cannot override a decorated method
- Modifier variable: The modified variable becomes a constant, and the constant cannot change during operation.
On a transformed object: a reference to the object created by the handle class is assigned to the object created by the parent class to get the upper transformation object. On a transform object cannot manipulate the new member variables and methods of a subclass, but you can manipulate member variables that are inherited or hidden by subclasses, and invoke methods inherited by subclasses or overridden instance methods.
If a subclass overrides an instance method of a parent class, the on-transition object invokes an instance method that is overridden by the subclass.
Note that if a subclass overrides a static method of a parent class, the upper transformation object of the child class object cannot invoke the overridden static method of the subclass, only the static method of the parent class can be called.
Polymorphism: The method of the parent class when overridden by a quilt class, can each produce its own functional behavior.
- Keyword abstract:
- Modification method: The decorated method can only declare that it cannot run.
- Modified class: There can be methods or methods in the class being decorated
abstract
非abstract
. Classes that are not modified can only have 非abstract
methods.
final
abstract
a method or class is not allowed and decorated at the same time.
The adornment method is not allowed static
abstract
. (The abstract method must be an instance method)
abstract
The child class of the parent class 非abstract
must override the method of the parent class abstract
.
abstrcat
Class can only declare an object but cannot create an object, but the object may become the upper-transition object of its subclass.
To transform objects and objects on:
Use keywords interface
to define an interface. 接口=接口声明+接口体
.
Only constants (with final
adornments, permissions public
, and static
constants) and abstract methods ( public
and abstract
adornments) are used in the interface body.
implements
declaring the class with a keyword in a class declaration implements one or more interfaces.
If a non-abstract class implements an interface, the class must override all methods in the interface. When overriding an interface method, remove the abstract
modifier, give the method body, and public
modify it.
If an abstract class implements an interface, it can either override the methods in the interface or directly own the methods in the interface.
You can use the interface name to access the constants in the interface, or you can do so directly by implementing the class of the interface.
public
An interface can be implemented by any class, and a friendly interface can be implemented by an interface in the same package.
Interfaces can also be extends
inherited, and subinterfaces inherit all the methods and constants of the parent interface.
The import statement can introduce the package's class or the interface to the package.
Interface callback: Simply put, the interface callback is to be able to invoke a method in an interface. Concrete operation: Class A implements interface B;a creates object a;b declares variable b;b=a;
abstract
Class-To-interface comparisons:
- Interfaces and
abstract
classes can be used in both abstract
methods
- Interfaces can only have constants, cannot have variables, and
abstract
classes may have constants or variables
abstract
A class can have non- abstract
methods, and interfaces cannot.