如果你要创建一个水果类,可以先定义什么是水果

/**

*用abstract关键字来定义水果,里面所有用abstract修饰的方法都是定义水果的,不需要写方法体,由非抽象的子类来实现且必须实现该方法

*/

public abstract class Fruits{

  String color = "黄色";

  public abstract void juice();

  public void color(){

  System.out.println(""+color);

}

 

}

/**

*用extends来继承

*

优点:
* 1、提高了代码的复用性
* 2、增加类与类之间的关系,让类呈现不同的形态,这也是Java三大特性之一多态的表现
* Java继承规则:
* 一个Java类最多只能继承一个父类,(单继承)
* 但是java支持多重继承
*
*
* 子父类构造方法特点:
* 1、在子类构造方法中第一行有一个隐式语句(super())
* 2、子类中所有的构造函数默认都会访问父类无参构造函数,
* 当子类构造函数有使用this调用其他构造函数(调用super()),则不用在引用父类构造方法
*

*/

public class apple extends Fruits{

public void juice(){

System.out.println("做果汁");      //方法重写:返回值类型相同,形参列表一致,方法名相同,子类不能降低父类的访问级别

}

String color = "红色";         //父类在子类中的成员变量的隐藏

}

/**

*用final修饰的类不能被继承,用final修饰的变量值不能被修改,用final修饰的方法不能被重写

/*
* 使用父类声明,子类实例化,这个对象就叫上转型对象。
*使用上转型对象可以扩展参数范围,体现了多态

*如父类是Iphone,子类是Iphone1:Iphone phone = new Iphone1();

*只可以用phone使用子类中重写或继承的方法以及被隐藏或继承的成员变量

*/

/**

*用public interface <接口名>进行接口的创建,用implements调用接口一个类可以调用多个接口如public class Apple extends Fruits implements Eating,Washing{}//其中Eating与Washing皆为接口

*用接口可以实现新功能,如手机使用接口调用电子支付功能,因为不是所有手机都需要这个功能,所以可以用需要这个功能的手机去继承原来的手机并调用接口

*接口中默认定义的方法为public abstract类型,成员变量为public static final类型

*在接口中所有的方法只能声明不能实现。

*/

接口回调:
接口声明,实现类实例化的对象叫做接口回调。
接口回调是指可以把实现某一接口的类创建的对象赋给该接口声明的接口变量中。
那么该接口变量就可以调用被类实现的接口中的方法。
实际上,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法

 这与上转型对象类似

例题:

/**
*创建一个抽象类水果Fruit,再分别创建Apple、Pear、Orange类继承抽象类水果,
*分别在3个子类中定义成员变量和方法,
*然后再创建一个测试类Test,根据用户的输入,利用上转型对象分别实例化不同的子类,表现多态的性质。
*/

package com.xt.homework09;
/**
* 创建一个水果类作为父类
* @author LSHC
*
*/
public class Fruits {
String color="橙色";
String size;
String name;
public void name(){
System.out.println(this.name + "");
}
public void eating(){
System.out.println("直接吃");
}
public void juice(){
System.out.println("做果汁");
}
}

package com.xt.homework09;
/**
* 创建苹果类
* @author LSHC
*
*/
public class Apple extends Fruits{
String color = "红色";
String size = "圆形";
String name = "苹果";

public void eating(){
System.out.println("削皮吃");
}


}

package com.xt.homework09;
/**
* 创建一个橙子类
* @author LSHC
*
*/
public class Orange extends Fruits{
String size = "圆形";
String name = "橙子";
public void eating(){
System.out.println("剥皮吃");
}

}

package com.xt.homework09;
/**
* 创建梨类
* @author LSHC
*
*/
public class Pear extends Fruits{
String color = "黄色";
String size = "梨形";
String name = "梨";
public void eating(){
System.out.println("洗洗吃");
}
}


public void eating(Fruits a){
a.eating();
}
public void juice(Fruits a){
a.juice();
}
public void color(Fruits a){
System.out.println(""+a.color);
}
public static void size(Fruits a){
System.out.println(""+ a.size);
}


public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入水果名:");
String name1 = scan.next();
Test test = new Test();
Fruits apple = new Apple();
Fruits orange = new Orange();
Fruits pear = new Pear();
String fruit_01 = "Apple";
String fruit_02 = "apple";
String fruit_03 = "苹果";
String fruit_04 = "梨" ;
String fruit_05 = "Orange";
String fruit_06 = "orange";
String fruit_07 = "橙子";
String fruit_08 = "Pear";
String fruit_09 = "pear";

if(name1.equals(fruit_01) || name1.equals(fruit_02) || name1.equals(fruit_03)){
apple.name();
test.color(apple);
test.size(apple);
test.juice(apple);
test.eating(apple);
}else if(name1.equals(fruit_04) || name1.equals(fruit_08) || name1.equals(fruit_09)){
pear.name();
test.color(pear);
test.size(pear);
test.juice(pear);
test.eating(pear);
}if(name1.equals(fruit_07) || name1.equals(fruit_06) || name1.equals(fruit_05)){
orange.name();
test.color(orange);
test.size(orange);
test.juice(orange);
test.eating(orange);
}
}

}

 

posted on 2017-04-02 00:24  BabyLoveRose  阅读(565)  评论(0编辑  收藏  举报