super调用父类的属性方法
super:可以用来修饰属性 方法 构造器
当子类与父类中有同名的属性时,可以通过 super.此属性 显式的调用父类声明的属性
若想调用子类的同名的属性“this.此属性”
2.当子类重写父类的方法以后,在子类中若想再显式的调用父类的被重写的方法,就需要使用 super.方法
3.super修饰构造器:通过在子类中使用“super(形参列表)”来显示的调用父类中指定的构造器
>在构造器内部,super(形参列表) 必须声明在首行
>在构造器内部,this(形参列表)或super(形参列表) 只能出现一个
>当构造器中,不是显示调用this(形参列表) 或super(形参列表)其中任何一个默认调用的是父类空参的构造器
建议:当设计一个类时,尽量要提供一个空参的构造器。
Person:
package com.aff.sup;
public class Person {
protected String name;
private int age;
int id = 1001;// 身份证号
public Person() {
this.name = "AA";
this.age = 1;
}
public Person(String name) {
this();
this.name = name;
}
public Person(String name, int age) {
this(name);
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void eat() {
System.out.println("eating");
}
public void sleep() {
System.out.println("sleeping");
this.eat();
}
// 比较当前对象与形参的对象的age谁大
public int compare(Person p) {
if (this.age > p.age) {
return 1;
} else if (this.age < p.age) {
return -1;
} else {
return 0;
}
}
}
Student:
package com.aff.sup;
public class Student extends Person {
private String schoolName;
int id = 1002;// 学号
public Student() {
super();
}
public void eat(){
System.out.println("学生吃饭");
}
public void info(){
this.eat();
super.eat();
super.sleep();//写成this.sleep也行 反正只有一个
}
public void show() {
System.out.println(this.id);//
System.out.println(super.id);// 1001 调用父类的id
System.out.println(super.name);
}
}
TestStudent:
package com.aff.sup;
public class TestStudent {
public static void main(String[] args) {
Student s = new Student();
s.show();
s.info();
}
}
输出结果:
1002
1001
AA
学生吃饭
eating
sleeping
学生吃饭
super调用父类的属性方法的更多相关文章
- python__基础 : 类的继承,调用父类的属性和方法
1.继承,调用父类属性方法 在python里面,继承一个类只需要这样写: class Animal: def heshui(self): print('动物正在喝水') class Cat(Anima ...
- python使用super()调用父类的方法
如果要在子类中引用父类的方法,但是又需要添加一些子类所特有的内容,可通过类名.方法()和super()来调用父类的方法,再个性化子类的对应函数. 直接使用类名.方法()来调用时,还是需要传入self为 ...
- 第7.22节 Python中使用super调用父类的方法
第7.22节 Python中使用super调用父类的方法 前面章节很多地方都引入了super方法,这个方法就是访问超类这个类对象的.由于super方法的特殊性,本节单独谈一谈super方法. 一.su ...
- C#中子类调用父类的实现方法
这篇文章主要介绍了C#中子类调用父类的实现方法,通过实例逐步分析了类中初始化构造函数的执行顺序问题,有助于加深对C#面向对象程序设计的理解,需要的朋友可以参考下 本文实例讲述了C#中实现子类调 ...
- 【转】子类会调用父类的@PostConstruct方法
如果一个类用@Service或@Component,那么只需要用@PostConstruct修饰某个方法,该方法能在类实例化的过程中自动执行,相当于类的构造函数.同时,具备了构造函数不具备的功能. @ ...
- super()调用父类构造方法
super()表示调用父类中的构造方法 1.子类继承父类,子类的构造方法的第一行,系统会默认编写super(),在调用子类的构造方法时,先调用父类的无参数构造方法 2.如果父类中只有有参数构造方法,那 ...
- 使用super调用父类的构造方法
package com.bjpowernode.t02inheritance.c09; /* * 使用super调用父类的构造方法 */public class TestSuper02 { publi ...
- [py]super调用父类的方法---面向对象
super()用于调用父类方法 http://www.runoob.com/python/python-func-super.html super() 函数是用于调用父类(超类)的一个方法. clas ...
- Java -- 子类使用super调用父类的方法A,A 调用了方法B,子类也override方法B,那么super.A()最终调用到了子类的B方法
public class SuperClass{ public void printA(){ System.out.print("SuperClass-printA"); prin ...
随机推荐
- libevent(六)http server
客户端: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signa ...
- STL之traits编程技法
traits编程技法利用了“内嵌型别”的编程技巧与编译器的template参数推导功能. 下面主要看看利用traits编程技法实现的迭代器萃取机制. 5种迭代器类型定义: struct input_i ...
- java23种设计模式——泡MM版
一.创建型模式 1.FACTORY:追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,固然口味有所不同,但不管你带MM往麦当劳或肯德基,只管向服务员说;来四个鸡翅就行了.麦当劳和肯德 ...
- Spring官网阅读(十八)Spring中的AOP
文章目录 什么是AOP AOP中的核心概念 切面 连接点 通知 切点 引入 目标对象 代理对象 织入 Spring中如何使用AOP 1.开启AOP 2.申明切面 3.申明切点 切点表达式 excecu ...
- LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)
977. Squares of a Sorted Array (Easy)# Given an array of integers A sorted in non-decreasing order, ...
- Linux Charger IC 驱动移植总结
Linux Charger IC 驱动移植总结 文章目录 Linux Charger IC 驱动移植总结 1 设备树的基本知识 设备树的概念 设备树的基本结构 compatible属性 举个栗子 2 ...
- 站在CSS3的肩上定义选择器
按上下文选择元素 按祖先元素选择要格式化的元素 输入ancestor,这里的ancestor是希望格式化的元素的祖先元素的选择器. 输入一个空格(必不可少). 如果需要,对后续的每个祖先元素重复第(1 ...
- Python中range, np.arange, np.linspace的区别
目录 range np.arange np.linspace range 特点 range()是python内置函数,指定开始值,终值和步长生成等差数列的一维数组 不包含终值 步长只能是整数,生成整数 ...
- Dotnet core使用JWT认证授权最佳实践(二)
最近,团队的小伙伴们在做项目时,需要用到JWT认证.遂根据自己的经验,整理成了这篇文章,用来帮助理清JWT认证的原理和代码编写操作. 第一部分:Dotnet core使用JWT认证授权最佳实践(一) ...
- ql的python学习之路-day3
字典操作 特性: 1.无序的 2.key是唯一的 , ,,], ,,], ,,], }, ,,], 'bbb' : ['a', 'b', 'c'], }}