十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
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();
}