2 Star 0 Fork 0

热门极速下载/hello-java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Polymorphism.java 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
Brais Moure 提交于 1个月前 . Clase 7 | 21/05/2025
package basic.c08_oop;
/*
Clase 7 - Polimorfismo, abstracción y composición (21/05/2025)
Vídeo: https://www.twitch.tv/videos/2464789369
*/
public class Polymorphism {
public static void main(String[] args) {
// Polimorfismo
// - Polimorfismo por herencia (sobrescritura)
var animal = new Animal();
animal.sound();
var dog = new Dog();
dog.sound();
// - Polimorfismo por sobrecarga (sobrecarga de métodos)
var calculator = new Calculator();
System.out.println(calculator.sum(3, 5));
System.out.println(calculator.sum(3.2, 5.4));
}
// - Polimorfismo por herencia (sobrescritura)
public static class Animal {
public void sound() {
System.out.println("Algún sonido");
}
}
public static class Dog extends Animal {
@Override
public void sound() {
System.out.println("Guau");
}
}
// - Polimorfismo por sobrecarga (sobrecarga de métodos)
public static class Calculator {
public int sum(int a, int b) {
return a + b;
}
public int sum(int a, int b, int c) {
return a + b + c;
}
public double sum(double a, double b) {
return a + b;
}
}
}
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

搜索帮助