package net.csdn.oop;
/**
* @author 彦舜
* oop(object-oriented programming 面向对象程序)
*/
//这个类用来测试继承的用法
public class Test01_UseExtends {
public static void main(String[] args) {
//创建实例化对象,测试对应类的功能
//创建父类对象测试
Father f = new Father();
f.eat();
//创建子类对象测试
Son s = new Son();
s.eat();
//s.count; 报错,不可直接使用:Syntax error, insert "AssignmentOperator Expression" to complete Expression
//调用子类继承过来的父类里成员变量:s.sum s.count
System.out.println(s.sum);
System.out.println(s.count);
s.study();
}
}
/** 1、编程思路:既然要测试继承,那么我们首先得创建父类,然后创建子类。
2、创建了抽象模板类,需要进行测试或使用,通过创建对应该类的实例化对象。*/
//创建父类
class Father{
int sum = 0;
int count = 20;
//在父类里写一个普通方法,于继承中测试成员变量的使用
public void eat() {
int count = 10;
System.out.println(count); //int count = 10;
//变量就近原则,局部变量与成员变量同名
System.out.println(count);
//局部变量与成员变量同名,要想使用成员变量,使用this关键字,其代表本类
继承中,this关键字与super关键字,globalVariable与localVariable,及个别报错代码细节
最新推荐文章于 2024-06-10 18:10:21 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)