java--inheritance and Super keyword

Source: Internet
Author: User

I. Parameter passing of methods in Java (emphasis)

Parameter Passing in Java is a value pass

Java中的值分两种:1、如果传递的参数是基本数据类型:  传递的值就是基本数据类型的值。   传递的时候,其实是把基本数据类型的值,复制一份,传递到方法内部2、如果传递的参数是引用数据类型:  传递的值是对象的地址值。   把地址值复制一份传递给方法的参数。
Ii. Object-oriented features 2: inheritance

Object-oriented three major features: 1, Package 2, inherit 3, polymorphic

Inherited syntax: Extends inherited classes

继承的特点:1、在继承的时候,被继承的类我们称之为父类或超类(superclass)、   基类(base class)2、继承别人的类,称之为子类。3、继承的时候,子类可以继承父类的所有的非私有成员! 私有成员无法继承。4、一个子类只能继承一个父类,不能同时继承多个父类。Java中的继承是单继承。 (亲爹只有一个!!!)5、一旦继承后,那么父类的非私有的成员,就成为了子类的成员。6、子类继承父类之后,也可以继续添加自己的属性和方法。 子类一般比父类具有更多的功能。7、一个父类可以有多个子类。8、java不支持多继承。 支持多重继承。b继承a, c可以继承b9、子类不能继承父类的构造方法!
Super keyword

1. Super key used in subclasses, you can access the specified properties or methods of the parent class. Relative This,this can only access the properties and methods of the current class.

注意:使用super的访问父类的属性和方法的时候,直接去父类中寻找,而不会在      子类中寻找。使用this访问当前对象的属性和方法的时候,是先在当前类中      寻找,如果当前找不到,再去父类中循环。

To understand super as an implicit reference to a parent class

2, when creating sub-class objects, the subclass of the construction method if not shown using this (...) To invoke the other constructor methods of this class. Then this constructor is bound to call the parent class's non-parametric construction method by default. You can use Super (argument) to display the specified constructor method of the calling parent class

 Note:  1, sub-class construction method neither this (...), there is no super (...).        Then the first line of the construction method is super ();  2, using super (...) When calling the constructor of the parent class, it must be placed on the first line of the constructor method.  3, within the same constructor method, you cannot use this () and --> subclass static code block --> the construction code block of the parent class --> Parent class construction method --> subclass Construction code block - Construction methods for subclasses static precedence --parent class precedence---> construct code block precedence---> construction method             
Key Words Properties Method Construction Method as a parameter
This Current properties Methods of the current object constructor method for the current class this () Pass as parameter
Super Parent Class Property Methods of the parent class constructor method for accessing the parent class super () cannot be used as a parameter
Third, access rights td> 
modifier same class same package subclass (different packages) Anywhere
private yes    
Default (defaults) Yes yes    
protected Yes Yes yes  
Public Yes Yes Yes Yes
注意:1、default有的时候,称之为包访问权限。只有同一个包中才可以访问。2、这四个权限都是用在类的成员中的。只能用到属性和方法。3、局部变量不允许添加权限修饰符。4、defalut默认访问权限,不建议使用。任何时候都要选择相应的访问修饰符, 不建议使用默认的。5、在子类中使用protected的属性或方法的时候,只能通过super或this来访问。 不能通过父类的对象来访问。 6、这四个访问权限,只能有两个可以用到外部类中。public、default。如果 一个类是使用的public的权限,那么类名必须和文件名保证一致。
Iv. overwrite of the method

When the function of the parent class's method does not meet the requirements of the subclass, the subclass can redefine the inherited method according to its business needs. Overwrite of method (override, override)

覆写的规则:两同、两小、一大两同:    方法名相同、参数列表相同两小:    1、子类的返回值类型必须是父类的返回值类型的子类或者与父类相同。        返回类型必须有子父类的关系。     2、子类抛出的异常,与父类相同,或者是父类异常类型的子类。一大:    子类的方法的访问权限,不能比父类小。
V. Characteristics of static methods in succession

Subclasses can inherit static methods of the parent class, but cannot overwrite the static methods of the parent class.

Vi. Final Keywords

Final can modify classes, variables, methods

1. Final Modified class: Represents this class and cannot have subclasses. Cannot be inherited. Eunuch class

2, Final modification method: that this method can not be covered by the quilt class cover, quilt class to inherit.

3. Final modified variable: This variable is a constant!!! This means that the constant can only be assigned once!!!

注意:1、final修饰成员变量的时候,必须手动赋值,不能让虚拟机赋默认值。2、形参也可以定义为final,但是这个时候不能再去手动赋值。他的值在调用方法的时候已经赋过值了。3、final修饰变量之后,变成常量,命名的时候,所有字母大写,不同的单词之间用下划线_连接。
VII. Abstract class

Classes that are decorated with abstract are called abstract classes.

Abstract method: Only method declaration, no method body, with the method of abstract modification.

抽象类的特点:1、抽象必须用abstract修饰。2、抽象类中可以有普通有的所有成员,另外他可以有抽象方法。3、抽象类不能直接创建对象!!!只能通过他的非抽象子类来创建对象。4、抽象类的子类,必须实行抽象类的所有抽象方法。如果不能完全实行父类的抽象方法,则必须把这个子类也标记为抽象类。5、抽象类可以没有抽象方法!但是,抽象方法必须放在抽象类中。6、抽象类和抽象方法能不能用final修饰? 不能7、抽象方法能不能是private的? 不能8、抽象方法能不能是protected? 能9、抽象方法能不能是static的? 不能
Eight or one war polymorphic
new Dog();两边类型不一致的情况,我们就称之为多态!Pet pet:称之为编译时类型    编译能否通过,看编译时类型。    书写代码的时候,能不能编译通过,看编译时类型有没有相应的方法。    new Dog():称之为运行时类型    运行的时候的具体结果,看运行时类型!
8.1 Types of conversions
自动类型转换    自动类型提升、向上转换    把子类类型的对象,赋值给父类的引用!    new Dog(); //自动类型转换强制类型转换    把父类类型的对象,赋值给子类类型的时候,需要强制类型转换!    能够强制类型转换,必须保证对象的实际类型,是要转换去的类型。         instanceof  用这个去判断某个引用是不是属于某个类型     用法:对象引用 instanceof 类

java--inheritance and Super keyword

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.