zoukankan      html  css  js  c++  java
  • java抽象类和抽象方法

    抽象类:

      java中定义没有方法体的方法,该方法有由其子类来具体的实现。

      该没有方法体的方法我们称为抽象方法,含有抽象方法的类为抽象类。

    抽象方法的特点:

      1.只有方法头没有方法体的方法

      2.抽象方法用abstract修饰

      3.抽象方法代表一种不确定的操作或行为

      4.抽象方法不能被调用

    抽象类的特点:

      1.定义中含有抽象方法的类叫抽象类

      2.抽象类用abstract修饰

      3.抽象类代表一种抽象的对象类型

      4.抽象类不能实例化

      5.抽象类可以有具体的方法,可以没有抽象方法

    //公司所有员工都有工号,姓名,工资,工作。
    //领导除了员工的属性和方法还有津贴
    public class EmployeeDemo {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		javaTeacher teacher = new javaTeacher(001,"张三",3000);
    		teacher.work();
    		leader lead = new leader(002,"李四",5000,10000);
    		lead.work();
    	}
    
    }
    
    abstract class employee{//抽象类
    	private int number;
    	private String name;
    	private double salary;
    	public abstract void work();//抽象方法
    	
    	public employee(int number,String name,double salary) {
    		this.number = number;
    		this.name = name;
    		this.salary = salary;
    	}
    	
    	//抽象类里不光可以有抽象方法,也可以有具体方法
    	public void aa(){
    		System.out.println("aaa"+number+name+salary);
    	}
    }
    
    //一个类继承抽象类,要么实现抽象类的方法,要么继续抽象下去
    class javaTeacher extends employee{
    	public javaTeacher(int number,String name,double salary) {
    		super(number,name,salary);
    	}
    	//抽象方法的实现
    	public void work() {
    		System.out.println("上java课");
    	}
    }
    
    class leader extends employee{
    	private double allowance;//津贴
    	public leader(int number,String name,double salary,double allowance) {
    		super(number,name,salary);
    		this.allowance = allowance;
    	}
    	//抽象方法的实现
    	public void work() {
    		System.out.println("培训新员工,津贴:"+allowance);
    	}
    }
    
  • 相关阅读:
    LeetCode 449. Serialize and Deserialize BST
    LeetCode Word Abbreviation
    LeetCode 402. Remove K Digits
    LeetCode 439. Ternary Expression Parser
    LeetCode Frog Jump
    LeetCode 630. Course Schedule III
    LeetCode 729. My Calendar I
    LeetCode 567. Permutation in String
    LeetCode Find Permutation
    LeetCode Number of Atoms
  • 原文地址:https://www.cnblogs.com/liubing2018/p/8439399.html
Copyright © 2011-2022 走看看