public class Demo{
public static void main(String[] args) {
Dog obj = new Dog();
obj.move();
}
}
class Animal{
private String desc = "Animals are human's good friends";
// 必须要声明一个 getter 方法
public String getDesc() { return desc; }
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
super.move(); // 调用父类的方法
System.out.println("Dogs can walk and run");
// 通过 getter 方法调用父类隐藏变量
System.out.println("Please remember: " + super.getDesc());
}
}
Animals can move Dogs can walk and run Please remember: Animals are human's good friends
public class Demo{
public static void main(String[] args) {
Dog obj = new Dog("花花", 3);
obj.say();
}
}
class Animal{
String name;
public Animal(String name){
this.name = name;
}
}
class Dog extends Animal{
int age;
public Dog(String name, int age){
super(name);
this.age = age;
}
public void say(){
System.out.println("我是一只可爱的小狗,我的名字叫" + name + ",我" + age + "岁了");
}
}
public final class Demo{
public static void main(String[] args) {
// 引用 People 类的实例
People obj = new People();
if(obj instanceof Object){
System.out.println("我是一个对象");
}
if(obj instanceof People){
System.out.println("我是人类");
}
if(obj instanceof Teacher){
System.out.println("我是一名教师");
}
if(obj instanceof President){
System.out.println("我是校长");
}
System.out.println("-----------"); // 分界线
// 引用 Teacher 类的实例
obj = new Teacher();
if(obj instanceof Object){
System.out.println("我是一个对象");
}
if(obj instanceof People){
System.out.println("我是人类");
}
if(obj instanceof Teacher){
System.out.println("我是一名教师");
}
if(obj instanceof President){
System.out.println("我是校长");
}
}
}
class People{ }
class Teacher extends People{ }
class President extends Teacher{ }
我是一个对象 我是人类 ----------- 我是一个对象 我是人类 我是一名教师
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有