欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Java抽象類(lèi)完整代碼,抽象JAVA

java程序,抽象類(lèi)實(shí)現(xiàn),求具體代碼

package test;

創(chuàng)新互聯(lián)是一家專(zhuān)注于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)與策劃設(shè)計(jì),中江網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:中江等地區(qū)。中江做網(wǎng)站價(jià)格咨詢:18980820575

/**

*

* @author JinnL

*父類(lèi)抽象類(lèi)

*/

public abstract class Car {

//轉(zhuǎn)彎

abstract void turn();

//啟動(dòng)

abstract void start();

void what(){

System.out.println("this is "+this.getClass().getSimpleName());

}

public static void main(String[] args) {

/**

* 方法入口

*/

Car[] cars ={new Bicycle(),new Automobile(),new GasAutomobile(),new DieselAutomobile()};

for (Car car : cars) {

car.start();

}

}

}

class Bicycle extends Car{

@Override

void turn() {

System.out.println("this is "+this.getClass().getSimpleName());

}

@Override

void start() {

System.out.println("this is "+this.getClass().getSimpleName());

}

void what(){

}

}

class Automobile extends Car{

@Override

void turn() {

System.out.println("this is "+this.getClass().getSimpleName());

}

@Override

void start() {

System.out.println("this is "+this.getClass().getSimpleName());

}

}

class GasAutomobile extends Automobile{

//重寫(xiě)start turn

@Override

void turn() {

System.out.println("this is "+this.getClass().getSimpleName());

}

@Override

void start() {

System.out.println("this is "+this.getClass().getSimpleName());

}

}

class DieselAutomobile extends Automobile{

@Override

void start() {

System.out.println("this is "+this.getClass().getSimpleName());

}

void what(){

System.out.println("this is "+this.getClass().getSimpleName());

}

}

java 抽象類(lèi)

我復(fù)制你的代碼 打印出來(lái)的是

王二,學(xué)生專(zhuān)業(yè)是:計(jì)算機(jī)科學(xué)

沒(méi)報(bào)錯(cuò)呢?

那不是報(bào)test2錯(cuò)誤么

求JAVA代碼~~~~~~~~~~:編寫(xiě)一個(gè)應(yīng)用抽象類(lèi)的程序。

//抽象的形狀類(lèi)

abstract class Shape{

abstract double getArea(); //抽象的求面積方法

}

//矩形類(lèi)

class Rectangle extends Shape{

protected double width;

protected double height;

public Rectangle(double width, double height){

this.width = width;

this.height = height;

}

@Override

double getArea() { //實(shí)現(xiàn)父類(lèi)的方法

return this.width * this.height;

}

}

//橢圓類(lèi)

class Ellipse extends Shape{

protected double a;

protected double b;

public Ellipse(double a, double b){

this.a = a;

this.b = b;

}

@Override

double getArea() {

return Math.PI * this.a * this.b;

}

}

public class TestAbstract {

public static void main(String[] args) {

Shape s;

s = new Rectangle(3, 4);

System.out.println("矩形的面積 : " + s.getArea());

s = new Ellipse(4, 3);

System.out.println("橢圓的面積 : " + s.getArea());

}

}

java請(qǐng)?jiān)O(shè)計(jì)一個(gè)使用抽象類(lèi)或接口的示例代碼

//接口

public?interface?BankCard?{

public?void?norm();

}

//工商銀行實(shí)現(xiàn)類(lèi)

public?class?ICBCBankCard?implements?BankCard?{

@Override

public?void?norm()?{

//?TODO?銀行規(guī)范

}

public?void?saveMoney(int?money){

//TODO?執(zhí)行存錢(qián)動(dòng)作?

}

public?void?transfer(String?account,int?money){

//TODO?執(zhí)行轉(zhuǎn)賬?動(dòng)作

}

}

//交通銀行實(shí)現(xiàn)類(lèi)

public?class?CommunicationsBankCard?implements?BankCard?{

@Override

public?void?norm()?{

//?TODO?銀行規(guī)范

}

public?void?saveMoney(int?money){

//TODO?執(zhí)行存錢(qián)動(dòng)作?

}

public?void?transfer(String?account,int?money){

//TODO?執(zhí)行轉(zhuǎn)賬?動(dòng)作

}

}

上面的例子只是申明了通用的規(guī)范,如果想讓實(shí)現(xiàn)類(lèi)都能實(shí)現(xiàn)存錢(qián)和轉(zhuǎn)賬功能,可以在接口里面聲明這兩個(gè)方法,寫(xiě)一個(gè)通用的實(shí)現(xiàn)類(lèi),實(shí)現(xiàn)這些方法,然后具體的子類(lèi)繼承該通用類(lèi),這樣可以直接繼承父類(lèi)方法,如果不同的銀行具體實(shí)現(xiàn)不同,可以復(fù)寫(xiě)父類(lèi)中的兩個(gè)方法。

編寫(xiě)一個(gè)Java代碼,在其中您將擁有一個(gè)帶有一個(gè)抽象方法的抽象類(lèi)。然后將抽象類(lèi)擴(kuò)展為兩個(gè)具體的子類(lèi)?

比較基礎(chǔ),給你個(gè)例子的思路:

1、創(chuàng)建抽象動(dòng)物類(lèi):AbstractAnimal.java:public AbstractAnimal{...},其中包含屬性String name;(自行設(shè)置getter和setter),包含抽象方法public void walk();

2、創(chuàng)建狗類(lèi)Dog.java,繼承抽象動(dòng)物類(lèi):public Dog extends AbstractAnimal{...},同時(shí)必須重寫(xiě)行走方法:

@Override

public void walk(){

System.out.println(super.name + "用四條腿走路");

}

3、創(chuàng)建人類(lèi)People.java,繼承抽象動(dòng)物類(lèi):public Peopleextends AbstractAnimal{...},同時(shí)必須重寫(xiě)行走方法:

@Override

public void walk(){

System.out.println(super.name + "用兩條腿走路");

}

4、編寫(xiě)測(cè)試類(lèi)

private static void main(String[] args){

AbstractAnimal dog = new God();

dog.setName("來(lái)福");

dog.walk();

AbstractAnimal people = new People();

people.setName("張三");

people.walk();

}

java 抽象類(lèi)是 什么

使用了關(guān)鍵詞abstract聲明的類(lèi)叫作“抽象類(lèi)”。如果一個(gè)類(lèi)里包含了一個(gè)或多個(gè)抽象方法,類(lèi)就必須指定成abstract(抽象)。“抽象方法”,屬于一種不完整的方法,只含有一個(gè)聲明,沒(méi)有方法主體。

抽象語(yǔ)法編輯

下面是抽象方法聲明時(shí)采用的語(yǔ)法:

abstract void f();

如果從一個(gè)抽象類(lèi)繼承,而且想生成新類(lèi)型的一個(gè)對(duì)象,就必須為基礎(chǔ)類(lèi)中的所有抽象方法提供方法定義。

如果不這樣做(完全可以選擇不做),則衍生類(lèi)也會(huì)是抽象的,而且編譯器會(huì)強(qiáng)迫我們用abstract 關(guān)鍵字標(biāo)志那個(gè)類(lèi)的“抽象”本質(zhì)。

即使不包括任何abstract 方法,亦可將一個(gè)類(lèi)聲明成“抽象類(lèi)”。如果一個(gè)類(lèi)沒(méi)必要擁有任何抽象方法,而且我們想禁止那個(gè)類(lèi)的所有實(shí)例,這種能力就會(huì)顯得非常有用。

在面向?qū)ο蟮母拍钪校覀冎浪械膶?duì)象都是通過(guò)類(lèi)來(lái)描繪的,但是反過(guò)來(lái)卻不是這樣。并不是所有的類(lèi)都是用來(lái)描繪對(duì)象的,如果一個(gè)類(lèi)中沒(méi)有包含足夠的信息來(lái)描繪一個(gè)具體的對(duì)象,這樣的類(lèi)就是抽象類(lèi)。抽象類(lèi)往往用來(lái)表征我們?cè)趯?duì)問(wèn)題領(lǐng)域進(jìn)行分析、設(shè)計(jì)中得出的抽象概念,是對(duì)一系列看上去不同,但是本質(zhì)上相同的具體概念的抽象。比如:如果我們進(jìn)行一個(gè)圖形編輯軟件的開(kāi)發(fā),就會(huì)發(fā)現(xiàn)問(wèn)題領(lǐng)域存在著圓、三角形這樣一些具體概念,它們是不同的,但是它們又都屬于形狀這樣一個(gè)概念,形狀這個(gè)概念在問(wèn)題領(lǐng)域是不存在的,它就是一個(gè)抽象概念。正是因?yàn)槌橄蟮母拍钤趩?wèn)題領(lǐng)域沒(méi)有對(duì)應(yīng)的具體概念,所以用以表征抽象概念的抽象類(lèi)是不能夠?qū)嵗摹?/p>

在面向?qū)ο箢I(lǐng)域,抽象類(lèi)主要用來(lái)進(jìn)行類(lèi)型隱藏。我們可以構(gòu)造出一個(gè)固定的一組行為的抽象描述,但是這組行為卻能夠有任意個(gè)可能的具體實(shí)現(xiàn)方式。這個(gè)抽象描述就是抽象類(lèi),而這一組任意個(gè)可能的具體實(shí)現(xiàn)則表現(xiàn)為所有可能的派生類(lèi)。模塊可以操作一個(gè)抽象體。由于模塊依賴于一個(gè)固定的抽象體,因此它可以是不允許修改的;同時(shí),通過(guò)從這個(gè)抽象體派生,也可擴(kuò)展此模塊的行為功能。熟悉OCP的讀者一定知道,為了能夠?qū)崿F(xiàn)面向?qū)ο笤O(shè)計(jì)的一個(gè)最核心的原則OCP(Open-Closed Principle),抽象類(lèi)是其中的關(guān)鍵所在。

2對(duì)比接口編輯

語(yǔ)法定義層

在abstract class方式中,Demo可以有自己的數(shù)據(jù)成員,也可以有非abstract的成員方法,而在interface方式的實(shí)現(xiàn)中,Demo只能夠有靜態(tài)的不能被修改的數(shù)據(jù)成員(也就是必須是static final的,不過(guò)在interface中一般不定義數(shù)據(jù)成員),所有的成員方法都是abstract的。

編程層面

abstract class在Java語(yǔ)言中表示的是一種繼承關(guān)系,一個(gè)類(lèi)只能使用一次繼承關(guān)系。但是,一個(gè)類(lèi)卻可以實(shí)現(xiàn)多個(gè)interface。

設(shè)計(jì)理念層面

abstract class在Java語(yǔ)言中體現(xiàn)了一種繼承關(guān)系,要想使得繼承關(guān)系合理,父類(lèi)和派生類(lèi)之間必須存在"is a"關(guān)系,即父類(lèi)和派生類(lèi)在概念本質(zhì)上應(yīng)該是相同的。 對(duì)于interface 來(lái)說(shuō)則不然,并不要求interface的實(shí)現(xiàn)者和interface定義在概念本質(zhì)上是一致的,僅僅是實(shí)現(xiàn)了interface定義的契約(功能)而已。

四、抽象類(lèi)的示例代碼

//: interfaces/music4/Music4.java

// Abstract classes and methods.

/* 抽象類(lèi)的功能

* 使類(lèi)的抽象性明確起來(lái)

*

*/

// 以下是 抽象類(lèi) 的示例

package interfaces.music4;

import pets.Pet;

import polymorphism.music.Note;

import static net.mindview.util.Print.*;

/* 1. 抽象方法 abstract void f(); 抽象方法不能包含有任何方法的BODY 。

* 2. 如果一個(gè)類(lèi)包含1個(gè)或者多個(gè)抽象方法, 則該類(lèi)必須限定為抽象的。

* 需要在前面指定 abstract 關(guān)鍵字。

* (1)抽象類(lèi)不能被實(shí)例化

* (2)包含抽象方法的類(lèi),必須標(biāo)識(shí) abstract

* 3. 如果從一個(gè)抽象類(lèi)繼承, 必須對(duì)所有抽象方法進(jìn)行覆蓋 , 否則導(dǎo)出類(lèi)也是抽象的

* 4. 也可以考慮創(chuàng)建沒(méi)有任何抽象方法的抽象類(lèi) 。

*

*/

abstract class Instrument {

// 抽象類(lèi)中可以有非抽象方法。

private int i; // Storage allocated for each

public abstract void play(Note n);

public String what() { return "Instrument"; }

public abstract void adjust();

}

/*

(2)包含抽象方法的類(lèi),必須標(biāo)識(shí) abstract, 否則編譯器會(huì)報(bào)錯(cuò)

class abc

{

public abstract void Demo();

}*/

class Wind extends Instrument {

public void play(Note n) {

print("Wind.play() " + n);

}

public String what() { return "Wind"; }

public void adjust() {}

}

class Percussion extends Instrument {

public void play(Note n) {

print("Percussion.play() " + n);

}

public String what() { return "Percussion"; }

public void adjust() {}

}

class Stringed extends Instrument {

public void play(Note n) {

print("Stringed.play() " + n);

}

public String what() { return "Stringed"; }

public void adjust() {}

}

class Brass extends Wind {

public void play(Note n) {

print("Brass.play() " + n);

}

public void adjust() { print("Brass.adjust()"); }

}

class Woodwind extends Wind {

public void play(Note n) {

print("Woodwind.play() " + n);

}

public String what() { return "Woodwind"; }

}

class TestAbsExt extends Instrument

{

public void play( Note n ) {

print ("TestAbsExt.play()" + n);

}

public String what()

{

return "TestAbsExt";

}

public void adjust()

{

}

}

// 不含任何抽象方法的抽象類(lèi)

abstract class AbsClass

{

public void f1() {

print("f1()");

}

public void f2() {

print("f2()");

}

}

public class Music4 {

// Doesn't care about type, so new types

// added to the system still work right:

static void tune(Instrument i) {

// ...

i.play(Note.MIDDLE_C);

}

static void tuneAll(Instrument[] e) {

for(Instrument i : e)

tune(i);

}

public static void main(String[] args) {

// !抽象類(lèi)不能被實(shí)例化

// 若使用以下語(yǔ)句 , 將會(huì)收到報(bào)錯(cuò)。

/* Exception in thread "main" java.lang.Error: Unresolved compilation problem:

Instructment cannot be resolved to a type

*/

// ! new Instructment();

// Upcasting during addition to the array:

Instrument[] orchestra = {

new Wind(),

new Percussion(),

new Stringed(),

new Brass(),

new Woodwind()

};

tuneAll(orchestra);

}

} /* Output:

Wind.play() MIDDLE_C

Percussion.play() MIDDLE_C

Stringed.play() MIDDLE_C

Brass.play() MIDDLE_C

Woodwind.play() MIDDLE_C

*///:~

/*

* 抽象方法不能包含有任何方法的BODY 。

*/

/*

abstract class RodentAbs extends Pet {

abstract public Rodent(String name) { super(name); }

abstract public Rodent() { super(); }

} ///:~

*/

文章標(biāo)題:Java抽象類(lèi)完整代碼,抽象JAVA
地址分享:http://chinadenli.net/article13/dsiedgs.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)標(biāo)簽優(yōu)化手機(jī)網(wǎng)站建設(shè)動(dòng)態(tài)網(wǎng)站移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)