类的继承(super)
Created|Updated|programming languages
|Post Views:
前言
我们知道子类能够改写父类的方法(例如叫:do),然后覆盖.从此,用子类调用do方法只能调那个被改写的do,如果我想用子类调用那个被覆盖的do怎么办呢?
OK,就要用super关键字
super
super关键字,能在子类中调用父类的属性,方法,和构造方法
- super调用父类的构造方法
1 | public abstract class Test { |
1 | public class Test2 extends Test{ |
因为构造方法中没有参数,所以可以不写编译器会自动,调用父类的无参构造方法
(如果有就要写构造方法,并在括号中加上参数)
- super调用父类属性
1 | this.name = super.name; |
- 调用父类的方法
1 | super.hello(); |
注意
java中
一个类只能有一个父类
子类不仅覆盖了父类的方法还会覆盖父类的属性
子类能使用父类的属性和方法,还能添加属性和方法,还能重写父类的属性和方法
Related Articles
2020-03-26
java面向对象思想(this,return)
前言面向对象的思想在主方法中,主要实现: 建对象 给参数 要什么方法就调用 了解 类的成员变量:定义在方法外可以被类方法访问的是成员变量。按照java的编码规范,成员变量一般被定义成private权限。 类的属性:类的成员变量中,外界可以通过set方法和get方法对成员变量进行操作,那么这个成员变量就是类的属性。 this.这个关键字主要用于引用本类中的成员变量或者方法将局部变量的值传递给成员变量给成员变量是为了给本类中的方法使用由于有时类中的属性名和方法中的参数名有时相同,所以目的为了区分类的属性和参数两者. 12345Public Class Student { String name; //定义一个成员变量name private void SetName(String name){ //定义一个参数(局部变量)name this.name = name; //将局部变量的值传递给成员变量} 实例123456789101112131415161718192021package 矩形面积;public class...
2020-12-13
java syntax
Basic conception First, every line of the code must be in the class. Second, a java file can have more than one class. However, there can only be one public class per java file and the name of public class must be the same name as its file name. VariablesA class can contain any of the following variable types. Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the...
2020-11-14
万年历
输入任何一年,2020则输出该年的所有月历表 输入任何年月,2020 11,则输出该月的月历表 输入任何年月日,2020 11 14,则输出该天是星期几 已知年月日,求星期几 int w=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7; 判断是闰年 (year%400 == 0 ) || (year%4==0 && year%100 != 0) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110import java.util.Scanner;public class Demo...
2020-03-25
反转排序算法
目的将一个数组反向排序 主要思想 将数组除以2,因为舍弃小数的缘故,所以适合将前面一半与后面一半调换。 示例1234567891011121314151617181920package 反转排序;public class 反转排序 { public static void main(String[] args) { int arr[] = new int[] {1, 2, 3, 4, 5}; int len = arr.length; int temp; for (int i = 0; i <= len / 2; i++) {//只需要除2 temp = arr[i]; arr[i] = arr[len - i - 1]; arr[len - i - 1] = temp; } for (int a : arr) System.out.print(a + " "); }} 注意 数组的下标从0开始,到length - 1结束
2020-03-29
上下转型
前言 为什么要向上转型?我们直接用继承父类的子类实例化子类对象不就好了吗?这不就可以既能访问子类的对象,也能访问父类对象了吗?如果我们要用被覆盖的父类的方法或者属性,那么用super调用不就好了吗? 介绍向上转型 就是将子类对象赋值给父类类型的变量。 向上转型是为了用父类变量来接受不同的子类对象,调用方法的时候传参父类对象,可以调用子类里不同的重写方法,得到不同的结果。 1Person tom = new Student(); 应用父类12345678910111213public class Animal { public void sleep() { System.out.println("小动物在睡觉"); } public static void doSleep(Animal animal) { // 此时的参数是父类对象,但是实际调用时传递的是子类对象,就是向上转型。 animal.sleep(); } public...
2020-03-29
多态
介绍多态就是一种事物的多种形态。 存在的条件 继承 重写 向上转型(父类引用指向子类对象) 实例123456789101112131415161718192021222324252627282930313233343536373839class Animal{ public Animal(String kinds) {//构造方法 System.out.println("创建一个" + kinds); } void move() { //父类的方法 }}class Fish extends Animal{ public Fish(String kinds) {//子类的构造方法 super(kinds); } public void move() { //改写父类的方法 System.out.println("swim"); }}class Dog extends...
