2 Star 0 Fork 0

热门极速下载/hello-java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Inheritance.java 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
Brais Moure 提交于 30天前 . Clase 7 | 21/05/2025
package basic.c08_oop;
/*
Clase 6 - Clases, encapsulamiento y herencia (14/05/2025)
Vídeo: https://www.twitch.tv/videos/2459212698
*/
public class Inheritance {
public static void main(String[] args) {
// Herencia ("es un")
var animal = new Animal("Mi animal");
// animal.name = "Mi animal";
animal.eat();
var dog = new Dog("Mou", 3);
// dog.name = "Mou";
dog.eat();
var cat = new Cat("Cou");
// cat.name = "Cou";
cat.eat();
var bird = new Bird("Bou");
// bird.name = "Bou";
bird.eat();
bird.fly();
}
public static class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println("El animal con nombre " + name + " está comiendo.");
}
}
public static class Dog extends Animal {
int age;
public Dog(String name, int age) {
super(name);
this.age = age;
}
@Override
public void eat() {
System.out.println("El perro con nombre " + name + " está comiendo.");
}
}
public static class Cat extends Animal {
public Cat(String name) {
super(name);
}
}
public static class Bird extends Animal {
public Bird(String name) {
super(name);
}
public void fly() {
System.out.println("Está volando");
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_trending/hello-java.git
git@gitee.com:mirrors_trending/hello-java.git
mirrors_trending
hello-java
hello-java
main

搜索帮助