• 抽象类


    1. 用Abstract关键字来修饰一个类时,该类称为抽象类;用Abstract关键字来修饰一个方法时,该方法称为抽象方法;

    2. 含有抽象方法的类必须被声明为抽象类,抽象类必须被继承,抽象方法必须被重写;

    3. 抽象类不能被实例化;

    4. 抽象方法只需要声明,不需要实现;

    5.Demo

    Demo_1

    class Animal{
    	private String name;
    
    	public Animal(String name) {
    		super();
    		this.name = name;
    	}
    	
    	public abstract void enjoy();
    }
    // 错误
    

     Demo_2

    abstract class Animal{
    	private String name;
    
    	public Animal(String name) {
    		super();
    		this.name = name;
    	}
    	
    	public abstract void enjoy();
    }
    // 正确
    

     Demo_3

    abstract class Animal{
      private String name;
    
      public Animal(String name) {
    	super();
    	this.name = name;
      }
      public abstract void enjoy();
    }
    class Cat extends Animal{
      private String furColor;
      public Cat(String name, String furColor) {
    	super(name);
    	this.furColor = furColor;
      }
      @Override
      public void enjoy() {
    	// TODO Auto-generated method stub	
      }
    }
    class Dog extends Animal{
      private String eyesColor;
      public Dog(String name, String eyesColor) { super(name); this.eyesColor = eyesColor;   }   @Override   public void enjoy() {  // TODO Auto-generated method stub   } } public class Test2 {   public static void main(String[] args) { // TODO Auto-generated method stub Dog d = new Dog("bigYelwow","Yellow"); // 正确 Animal a = new Animal("cat"); // 错误   } }
  • 相关阅读:
    .Net之美读书笔记15
    WinForm跨线程访问控件异常
    .Net之美读书笔记14
    数据库监视器(SQL Server Profilter)
    .Net之美读书笔记13
    .Net之美读书笔记11
    .Net之美读书笔记9
    .Net之美读书笔记8
    tensorflow:验证码的识别(中)
    tensorflow:验证码的识别(上)
  • 原文地址:https://www.cnblogs.com/bosongokay/p/6743865.html
Copyright © 2020-2023  润新知