• java abstract


    简介

    java 中的抽象类学习

    code

    main

    package com;
    
    public class PersonTest {
    	public static void main(String[] args) {
    		Person[] person = new Person[2];
    		
    		person[0] = new Employee("Harry Hacker", 5000, 1989, 10,1);
    		person[1] = new Student("Maria Morris", "computer science");
    		
    		for(Person p : person)
    			System.out.println(p.getName() + ", " + p.getDescription());
    	}
    }
    
    

    abstract

    package com;
    
    public abstract class Person {
    	public abstract String getDescription();
    	private String name;
    	
    	public Person(String name) {
    		this.name = name;
    	}
    	
    	public String getName() {
    		return name;
    	}
    }
    

    Student

    package com;
    
    public class Student extends Person{
    	private String major;
    	/**
    	 * @param name the student's name
    	 * @param major the student's major 
    	 */
    	public Student(String name, String major) {
    		super(name);
    		this.major = major;
    	}
    	public String getDescription(){
    		return "a student majoring in " + major;
    	}
    }
    

    Employee

    package com;
    
    import java.util.Random;
    import java.time.*;
    class Employee extends Person{
    	private double salary;
    	private LocalDate hireDay;
    	
    	public Employee(String name, double salary, int year, int month, int day){
    		super(name);
    		this.salary = salary;
    		hireDay = LocalDate.of(year, month, day);
    	}
    	
    	public double getSalary() {
    		return salary;
    	}
    	
    	public LocalDate getHireDay() {
    		return hireDay;
    	}
    	
    	public String getDescription() {
    		return String.format("an emplyee with a salary of $%.2f", salary);
    	}
    	
    	public void raiseSalary(double byPercent) {
    		double raise = salary * byPercent / 100;
    		salary += raise;
    	}
    }
    

    自问自答环节

    QU: 抽象类能不能生成对象
    AN:不能
    QU:某类继承了抽象类,在什么情况加某类才不是抽象类
    AN:实现了抽象类里面的函数覆盖

    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    UML的现状及未来发展
    终于申请好了Blog。:)
    2004年最后一场雪
    今天开始在乐世安通上班了
    狐狸与兔子问题
    今天上午
    好久没更新了啊,写写近况
    还是得考研啊!
    kettle HTTP client
    国外的一个PIC源代码网站,比较不错,基于HiTech C的。共享一下
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13388218.html
Copyright © 2020-2023  润新知