重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
package test;
成都創新互聯主要從事做網站、網站建設、網頁設計、企業做網站、公司建網站等業務。立足成都服務日照,10余年網站建設經驗,價格優惠、服務專業,歡迎來電咨詢建站服務:18982081108
/**
*
* @author JinnL
*父類抽象類
*/
public abstract class Car {
//轉彎
abstract void turn();
//啟動
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{
//重寫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());
}
}
//抽象的形狀類
abstract class Shape{
abstract double getArea(); //抽象的求面積方法
}
//矩形類
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() { //實現父類的方法
return this.width * this.height;
}
}
//橢圓類
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());
}
}
比較基礎,給你個例子的思路:
1、創建抽象動物類:AbstractAnimal.java:public AbstractAnimal{...},其中包含屬性String name;(自行設置getter和setter),包含抽象方法public void walk();
2、創建狗類Dog.java,繼承抽象動物類:public Dog extends AbstractAnimal{...},同時必須重寫行走方法:
@Override
public void walk(){
System.out.println(super.name + "用四條腿走路");
}
3、創建人類People.java,繼承抽象動物類:public Peopleextends AbstractAnimal{...},同時必須重寫行走方法:
@Override
public void walk(){
System.out.println(super.name + "用兩條腿走路");
}
4、編寫測試類
private static void main(String[] args){
AbstractAnimal dog = new God();
dog.setName("來福");
dog.walk();
AbstractAnimal people = new People();
people.setName("張三");
people.walk();
}