簡單學(xué)習(xí)Java抽象類要點(diǎn)及實(shí)例
使用抽象類應(yīng)該注意的幾個(gè)要點(diǎn):
包含一個(gè)或者多個(gè)抽象方法的類必須被聲明為抽象類.
將類聲明為抽象類,不一定含有抽象方法.
通常認(rèn)為,在抽象類中不應(yīng)該包括具體方法,建議盡量將通用的域和方法放在超類中.
抽象類不可以被實(shí)例化.即不能創(chuàng)建這個(gè)類的對象
實(shí)例代碼:
import java.util.*;
/**
* This program demonstrates abstract classes.
* @version 1.01 2004-02-21
* @author Cay Horstmann
*/
public class PersonTest
{
public static void main(String[] args)
{
Person[] people = new Person[2];
// fill the people array with Student and Employee objects
people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
people[1] = new Student("Maria Morris", "computer science");
// print out names and descriptions of all Person objects
for (Person p : people)
System.out.println(p.getName() + ", " + p.getDescription());
}
}
abstract class Person
{
public Person(String n)
{
name = n;
}
public abstract String getDescription();
public String getName()
{
return name;
}
private String name;
}
class Employee extends Person
{
public Employee(String n, double s, int year, int month, int day)
{
super(n);
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
hireDay = calendar.getTime();
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public String getDescription()
{
return String.format("an employee with a salary of $%.2f", salary);
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
private double salary;
private Date hireDay;
}
class Student extends Person
{
/**
* @param n the student's name
* @param m the student's major
*/
public Student(String n, String m)
{
// pass n to superclass constructor
super(n);
major = m;
}
public String getDescription()
{
return "a student majoring in " + major;
}
private String major;
}
在代碼塊:
for (Person p : people)
System.out.println(p.getName() + ", " + p.getDescription());
中p.getDescription(),將引用具體子類的子類對象的方法.
不可以省略Person類中的getDescription(),原因是編譯器只允許調(diào)用在類中聲明的方法.
相關(guān)文章
java 進(jìn)程是如何在Linux服務(wù)器上進(jìn)行內(nèi)存分配的
這篇文章主要介紹了java 進(jìn)程是如何在Linux服務(wù)器上進(jìn)行內(nèi)存分配的,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11解決springboot項(xiàng)目找不到resources目錄下的資源問題
這篇文章主要介紹了解決springboot項(xiàng)目找不到resources目錄下的資源問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java實(shí)現(xiàn)整數(shù)的逆序輸出的三種方法
這篇文章主要介紹了Java實(shí)現(xiàn)整數(shù)的逆序輸出的三種方法,第一種是無限制整數(shù)的逆序輸出,第二種是非負(fù)整數(shù)的逆序輸出,第三種是非特殊情況的逆序輸出,每種方法給大家講解的非常詳細(xì)需要的朋友可以參考下2022-11-11如何開啟控制臺輸出mybatis執(zhí)行的sql日志問題
這篇文章主要介紹了如何開啟控制臺輸出mybatis執(zhí)行的sql日志問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09springboot 使用zookeeper實(shí)現(xiàn)分布式隊(duì)列的基本步驟
這篇文章主要介紹了springboot 使用zookeeper實(shí)現(xiàn)分布式隊(duì)列,通過ZooKeeper的協(xié)調(diào)和同步機(jī)制,多個(gè)應(yīng)用程序可以共享一個(gè)隊(duì)列,并按照先進(jìn)先出的順序處理隊(duì)列中的消息,需要的朋友可以參考下2023-08-08maven升級版本后報(bào)錯(cuò):Blocked mirror for repositories
本文主要介紹了maven升級版本后報(bào)錯(cuò):Blocked mirror for repositories,文中的解決方法非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09Java8新特性之重復(fù)注解(repeating annotations)淺析
這篇文章主要介紹了Java8新特性之重復(fù)注解(repeating annotations)淺析,這個(gè)新特性只是修改了程序的可讀性,是比較小的一個(gè)改動,需要的朋友可以參考下2014-06-06