java继承与覆盖_简单的继承,方法重载与方法覆盖

[java]代码库package com.jiarui;

public class Demo1 {

public static void main(String[] args) {

Dog dog1= new Dog(2,"大黄");

System.out.println(dog1.name+"的年龄为:"+dog1.getAge());

dog1.Cry();

Cat cat1=new Cat(3,"小花");

cat1.Cry();

System.out.println(cat1.name+"的年龄为:"+cat1.getAge());

Abc abc= new Abc();

System.out.println(abc.getNum(12,18));

}

}

class Animal{

int age;

String name;

public Animal(){

this.age=age;

this.name=name;

}

public void Cry(){

System.out.println("动物的叫声是:不知道。");

}

}

class Dog extends Animal{

//private int age;

//public String name;

public Dog(int age,String name){

this.age=age;

this.name=name;

}

//方法的覆盖

public void Cry(){

System.out.println("狗的叫声是:汪汪");

}

public int getAge(){

return this.age;

}

}

class Cat extends Animal{

public Cat(int age,String name){

this.age=age;

this.name=name;

}

public void Cry(){

System.out.println("猫的叫声是:喵喵!");

}

public int getAge(){

return this.age;

}

}

class Abc{

public int getNum(int i,int j){

if (i>j) {

return i;

}else {

return j;

}

}

//方法重载:只是返回类型不一样,不能构成重载

public float getNum(float a,float b){

if (a>b) {

return a;

}else {

return b;

}

}

}

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值