Java Abstract Class What Is It Good For?

Java Abstract Class What Is It Good For?

java 抽象類有什麼有點?

The Java abstract class eludes many Java developers. Let’s learn what it does for us and how to use it.

java抽象類使得很多java開發人員都很想逃避,讓我們學習它對我們有什麼作用。

Abstract art: a product of the untalented sold by the unprincipled to the utterly bewildered. Al Capp

抽象藝術:由無原則的人出售給完全困惑的人的一種產品。阿爾-卡普

I am guessing you have heard of the malady called ADD or Attention Deficit Disorder. On a recent trip to Paris, my son and I discovered we are suffering from another malady with similar initials. Art Deficit Disorder.

We can look at paintings and sculptures and find it uninspiring. Where my daughter enjoyed the d’Orsay we looked for the food court. Where we enjoyed some espresso and fresh-squeezed orange juice.

我猜你聽說過一種叫做注意力缺失症的疾病。在最近的一次巴黎之行中,我和兒子發現我們患上了另一種同名疾病。藝術缺陷障礙。

我們可以看看繪畫和雕塑,發現它毫無生氣。在我女兒喜歡奧賽的地方,我們找了美食廣場。我們在那裏喝了一些濃咖啡和鮮榨橙汁。

Java Abstract Class

Java抽象類

Java has abstract classes that are incomplete. They cannot be implemented like a regular class. Abstract classes must be subclassed to be used. In these classes, we can declare abstract methods. Abstract classes are similar to interfaces in Java. Let’s dive deeper into this comparison.

Java有不完整的抽象類它們不能像普通類那樣實現抽象類必須是子類才能使用。在這些類中,我們可以聲明抽象方法抽象類類似於Java中的接口讓我們更深入地研究一下這個比較。

Compare

比較

Like interfaces, abstract classes cannot be instantiated. Where the interface just will contain method signatures an abstract class can contain a method body. Abstract classes can declare fields that are not static and final.

與接口一樣,抽象類也不能實例化。如果接口只包含方法簽名,則抽象類可以包含方法體。抽象類可以聲明非靜態和最終的字段。

Where interfaces have all fields automatically become public, static, and final. We can implement as many interfaces as we want. Abstract classes are like regular classes and we can only extend one.

其中接口使所有字段自動成爲公共、靜態和最終字段。我們可以實現任意多個接口。抽象類就像常規類,我們只能擴展一個。

The Java tutorial has some good guidance when to use abstract classes. When we “want to share code among several closely related classes” or “expect that classes that extend your abstract class have many common methods or fields”. Interfaces should be used when “expect that unrelated classes would implement your interface” or “want to specify the behavior of a particular data type”.

Java教程對何時使用抽象類有很好的指導當我們“希望在幾個密切相關的類之間共享代碼”或“希望擴展抽象類的類有許多公共方法或字段”時。當“期望不相關的類實現您的接口”或“希望指定特定數據類型的行爲”時,應使用接口。

Java Abstract Class Example

java抽象類實例

Like all good coders let’s get our hands dirty with some code. First, we can look at an example abstract class to get us started.

像所有優秀的程序員一樣,讓我們用一些代碼弄髒我們的手。首先,我們可以看一個示例抽象類來了解

package com.myitcareercoach;

public abstract class Battery {
	int volt;
	int amps;
	void charge(int chargingTime) {
		// shared code
	}
	
	abstract boolean fullyCharged();
	
	abstract boolean isTooHot();
	
}
view rawBattery.java hosted with ❤ by GitHub

 

This Battery abstract class has one implemented method and two abstract methods. There are also two fields defined as well.

這個Battery抽象類有一個實現方法和兩個抽象方法還定義了兩個字段。

package com.myitcareercoach;

public class ComputerBattery extends Battery {

	@Override
	boolean fullyCharged() {
		// TODO Add some code here!
		return false;
	}

	@Override
	boolean isTooHot() {
		// TODO Add some code here!
		return false;
	}

}
view rawComputerBattery.java hosted with ❤ by GitHub

ComputerBattery is a concrete Java class. Therefore, it needs to implement both of the abstract methods that Battery defined.

ComputerBattery是一個具體的Java類因此,它需要實現battery定義的兩個抽象方法。

Abstract and Interface?

抽象類和接口?

An abstract class can even implement an interface. This seems like mixing spaghetti and mashed potatoes but, hey it is legal!

一個抽象類甚至能實現一個接口,這好像混合了意大利麪條和土豆泥,但是,嘿,這是合法的!

public interface Student {
      
       public void setSchedule();
      
       public void registerForClass(String className);

}
view rawStudent.java hosted with ❤ by GitHub

Let’s take our Student interface and mix it in an abstract class.

讓我們把學生界面混合到一個抽象的類中。

public abstract class ProbationaryStudent implements Student {

	@Override
	public void setSchedule() {
		// implemented method
	}

	// not going to override registerForClass() method

}
view rawProbationaryStudent.java hosted with ❤ by GitHub

In our ProbationaryStudent class, we don’t implement all the methods defined in the Student interface. This is possible since the class is marked as abstract.

在我們的試用學生課程中,我們沒有實現在學生界面中定義的所有方法。這是可能的,因爲類被標記爲抽象的。

Main?

主要的?

Would you think if you had the main method in an abstract class that it would run?

你認爲如果在抽象類中有main方法,它會運行嗎?

public abstract class DoesItRun {

	public static void main(String[] args) {
		System.out.println("Does it run?");
	}

}
view rawDoesItRun.java hosted with ❤ by GitHub

I didn’t think it would either but in fact, it does run. I suggest you try it out for yourself. As you can see abstract classes have their place in Java. Similar to interfaces but used in a different way.

Where have you used Java interfaces?

我也不認爲會,但事實上,它確實運行我建議你自己試試。正如您所看到的,抽象類在Java中佔有一席之地。類似於接口,但使用方式不同。

你在哪裏使用過Java接口?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章