In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
父类:
public class Animal {
int age; //年龄
double height; //身高
double weight; //体重
Animal()
{
System.out.println("一个动物诞生");
}
Animal(int age,double height,double weight)
{
this.age = age;
this.height = height;
this.weight = weight;
}
//睡觉方法
public void sleep()
{
System.out.println("动物在睡。。。。。");
}
}
子类:
package javas.frist;
//人类
public class Human extends Animal {
Human()
{
System.out.println("一个人诞生");
}
//睡觉方法
@Override //方法重写
public void sleep()
{
System.out.println("人类在开始睡觉啦。。。。。");
}
}
//子类鱼
public class Fish extends Animal{
//睡觉方法
public void sleep()
{
System.out.println("开着眼睛在睡觉。。。。。");
}
}
//子类马
public class Hose extends Animal {
//睡觉方法
public void sleep()
{
System.out.println("站者睡觉。。。。。");
}
}
测试类:
package javas.frist;
public class Test {
public static void main(String[] args) {
Human h = new Human(); //new 谁就子类方法就显示
// Human h = new Human();
Animal h = new Human(); //多态现象
//Animal h = new Animal(); //多态现象
System.out.println("年龄:"+h.age);
System.out.println("体重:"+h.weight);
System.out.println("身高:"+h.height);
h.sleep();
Animal a = new Animal();
a.sleep();
}
/**
* 生存子类对象,先执行父类构造方法,在执行子类构造方法
* 注意:构造方法是不能继承的。
* java采用单根继承,都是从object继承
* 继承好处:提高代码的复用
*
* 方法重写:Override /Overwrite,子类重写了从父类继承过来的方法
* 方法重载:Overload,方法的名字相同,参数个数,类型,次序不同
* 相同点:都是一种多态性的表现,方法重写属于一种动态的多态性,方法重载属于静态多态性
*
* 不同点,方法重写有继承,方法重载无继承。
*
* 多态三个条件:
*有继承、有方法重写、父类引用指向子类对象
*
*
*
* */
}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.