//定义一个鸟类
public class Bird {
public static void main(String[] args) {
Waterfowl w = new Waterfowl();
w.Fly();//继承父类飞行技能
w.Sleep();//继承父类睡眠技能
w.Swimming();//自己独特的技能
}
//飞行
public void Fly(){
System.out.println("鸟类会飞!");
}
//觅食
public void Eat(){
System.out.println("鸟会觅食!");
}
//睡觉
public void Sleep(){
System.out.println("鸟会睡觉!");
}
}
//水鸟,继承自鸟类
class Waterfowl extends Bird{
public void Swimming(){
System.out.println("水鸟会游泳!");
}
}程序输出:鸟会睡觉!
水鸟会游泳!
虽然水鸟类Waterfowl类实体内并没有定义飞行以及睡觉等方法,但是因为它与父类Bird的继承关系,所以父类的公用方法子类就可以公用,这样子类Waterfowl就不用再写代码去实现飞行和睡觉等功能了,代码的冗余度就会降低。那么,如果子类要重新定义飞行功能呢,那么就用到多态的。
//同一个类中,对方法Add重载
public class ExampleDemo {
public static void main(String[] args) {
Add(1,2);
Add(1.5,2.5);
Add(1.5,1);
Add(2,3.5);
Add("hello",9573);
Add(9573,"ABC");
}
public static void Add(int a,int b){
System.out.println("整数运算:"+a+"+"+b+" = "+(a+b));
}
public static void Add(double a, double b){
System.out.println("小数运算:"+a+"+"+b+" = "+(a+b));
}
public static void Add(double a, int b){
System.out.println("混合运算:"+a+"+"+b+" = "+(a+b));
}
public static void Add(int a, double b){
System.out.println("混合运算:"+a+"+"+b+" = "+(a+b));
}
public static void Add(String a, int b){
System.out.println("字符与数字连接:"+a+"+"+b+" = "+a+b);
}
public void Add(int a, String b){
System.out.println("字符与数字连接:"+a+"+"+b+" = "+a+b);
}
}小数运算:1.5+2.5 = 4.0
混合运算:1.5+1 = 2.5
混合运算:2+3.5 = 5.5
字符与数字连接:hello+9573 = hello9573
字符与数字连接:9573+ABC = 9573ABC
//子类A继承父类ExampleDemo,重写Add方法
class A extends ExampleDemo{
//当字符与数字混合运算时,将字符串反转后与数字连接再打印
public static void Add(int a,String b){
StringBuffer sb = new StringBuffer();
sb.append(b);
System.out.println(b+"反转后"+sb.reverse());
System.out.println(a+"+"+b+" = "+a+sb.toString());
}
public static void Add(String a, int b){
StringBuffer sb = new StringBuffer();
sb.append(a);
System.out.println(a+"反转后"+sb.reverse());
System.out.println(a+"+"+b+" = "+sb.toString()+b);
}
}
//子类B继承父类ExampleDemo,重写Add方法
class B extends ExampleDemo{
//当字符与数字混合运算时,将数字反转后与字符串连接再打印
public static void Add(int a,String b){
StringBuffer sb = new StringBuffer();
sb.append(a);
System.out.println(a+"反转后"+sb.reverse());
System.out.println(a+"+"+b+" = "+sb.toString()+b);
}
public static void Add(String a, int b){
StringBuffer sb = new StringBuffer();
sb.append(b);
System.out.println(b+"反转后"+sb.reverse());
System.out.println(a+"+"+b+" = "+a+sb.toString());
}
}
public static void main(String[] args) {
ExampleDemo E = new ExampleDemo();
ExampleDemo eA = new A();
ExampleDemo eB = new B();
A a = new A();
B b = new B();
E.Add("hello",9573);//1
eA.Add("hello",9573);//2
eB.Add("hello",9573);//3
a.Add("hello",1234);//4
a.Add(1.2, 2.8);//5
b.Add("hello",1234);//6
b.Add(1, 3.5);//7
}运行输出:public class ExampleDemo {
public static void main(String[] args) {
ExampleDemo E = new ExampleDemo();
ExampleDemo eA = new A();
ExampleDemo eB = new B();
A a = new A();
B b = new B();
E.Add("hello",9573);//1
eA.Add("hello",9573);//2
eB.Add("hello",9573);//3
a.Add("hello",1234);//4
a.Add(1.2, 2.8);//5
b.Add("hello",1234);//6
b.Add(1, 3.5);//7
}
public void Add(int a,int b){
System.out.println("整数运算:"+a+"+"+b+" = "+(a+b));
}
public void Add(double a, double b){
System.out.println("小数运算:"+a+"+"+b+" = "+(a+b));
}
public void Add(double a, int b){
System.out.println("混合运算:"+a+"+"+b+" = "+(a+b));
}
public void Add(int a, double b){
System.out.println("混合运算:"+a+"+"+b+" = "+(a+b));
}
public void Add(String a, int b){
System.out.println("字符与数字连接:"+a+"+"+b+" = "+a+b);
}
public void Add(int a, String b){
System.out.println("字符与数字连接:"+a+"+"+b+" = "+a+b);
}
}
//子类A继承父类ExampleDemo,重写Add方法
class A extends ExampleDemo{
//当字符与数字混合运算时,将字符串反转后与数字连接再打印
public void Add(int a,String b){
StringBuffer sb = new StringBuffer();
sb.append(b);
System.out.println(b+"反转后"+sb.reverse());
System.out.println(a+"+"+b+" = "+a+sb.toString());
}
public void Add(String a, int b){
StringBuffer sb = new StringBuffer();
sb.append(a);
System.out.println(a+"反转后"+sb.reverse());
System.out.println(a+"+"+b+" = "+sb.toString()+b);
}
}
//子类B继承父类ExampleDemo,重写Add方法
class B extends ExampleDemo{
//当字符与数字混合运算时,将数字反转后与字符串连接再打印
public void Add(int a,String b){
StringBuffer sb = new StringBuffer();
sb.append(a);
System.out.println(a+"反转后"+sb.reverse());
System.out.println(a+"+"+b+" = "+sb.toString()+b);
}
public void Add(String a, int b){
StringBuffer sb = new StringBuffer();
sb.append(b);
System.out.println(b+"反转后"+sb.reverse());
System.out.println(a+"+"+b+" = "+a+sb.toString());
}
}运行结果:
1497



被折叠的 条评论
为什么被折叠?



