[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;
}
}
}