抽象类

抽象类就是只声明方法的存在而不去具体实现它的类,抽象类必须被重写,必须被继承。抽象类不能被实例化,在定义抽象类时,要在关键字class前面加上关键字abstract。

``

abstract class Animals{
类名
}

import java.util.Scanner;

abstract class Animals{//抽象类定义
public String noise;//定义声音成员变量
public Animals(){
noise= “wawa”;//对变量noise进行初始化
}
public abstract void color();//定义抽象方法
}
class Cat extends Animals{//创建Animal子类Cat
private String name;
private int size;
public Cat(String name,int size){
this.name=name;
this.size=size;
}
@Override
public void color() {
// TODO Auto-generated method stub
System.out.println(“the cat color is white”+" “+this.name+” “+this.size);
}
}
class Dog extends Animals{//创建Animal子类Dog
private String name;
private int size;
public Dog(String name,int size){
this.name=name;
this.size=size;
}
@Override
public void color() {
// TODO Auto-generated method stub
System.out.println(“the dog color is black”+” “+this.name+” “+this.size);
@Override
public void color() {
// TODO Auto-generated method stub
System.out.println(“the dog color is black”+” “+this.name+” "+this.size);
}
}

public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
Cat cat=new Cat(sc.next(),sc.nextInt());//
cat.color(); //输出
Dog dog=new Dog(sc.next(),sc.nextInt());
dog.color();//输出
System.out.println(cat.noise);//利用cat调用父类抽象方法
System.out.println(dog.noise);//利用dog调用父类抽象方法
}
}

``

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值