适配器设计模式
为了解决接口的抽象方法过多。普通实现哼麻烦。就需要一个适配器类
多态
Object是所有类的根类,所有的类都是object的子类
直接打印对象实例输出的事tostring()方法返回值
java中向下转型必须要先向上转型
类的设计只要是父类的设计
子类最好不要去继承一个已经完全实现好了的类,因为一旦发生向上转型所调用的方法被子类覆盖过方法所以只会继承抽象类或者接口
/**
适配器设计模式:为解决接口的抽象方法过多,普通实现很麻烦,就需要一个适配器类
*/
class JavaDemo35
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
new CCC().test3();
}
}
interface AA
{
void test1();
void test2();
void test3();
void test4();
void test5();
}
//适配器类
abstract class CCCAA
{
public void test1(){
}
public void test2(){
}
public void test3(){
}
public void test4(){
}
public void test5(){
}
}
class CCC extends CCCAA
{
public void test3(){
System.out.println("test3");
}
}
/**
多态:
Object:是所有的类的根类,所有的都是Object的子类
直接打印对象实例输出的是toString()方法的返回值,默认getClass().getName() + '@' + Integer.toHexString(hashCode())
java中向下转型,必须要先向上转型。
*/
class JavaDemo36
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
Orange orange = new Orange();
System.out.println(orange);
new Person().eat(new Apple());
new Person().eat(new Banana());
Fruit f = new Apple();
Apple a = (Apple)f;
if(f instanceof Orange){
Orange Orange1 = (Orange)f;
}else{
System.out.println("我是不是神仙!");
}
//DD dd = new DD();
//EE ee = (EE)dd;
}
}
class Person
{
/*
public void eat(Apple temp){
System.out.println("吃:"+temp);
}
public void eat(Banana temp){
System.out.println("吃:"+temp);
}
*/
//多态
public void eat(Fruit temp){
System.out.println("吃:"+temp);
}
}
interface Fruit
{
}
class Orange implements Fruit
{
public String toString(){
return "橘子";
}
}
class Apple implements Fruit
{
public String toString(){
return "苹果";
}
}
class Banana implements Fruit
{
public String toString(){
return "香蕉";
}
}
class DD
{
}
class EE extends DD
{
}
class JavaDemo37
{
public static void main(String[] args)
{
AAA aaa = new CCC();
aaa.test();
}
}
class AAA
{
public void test(){
System.out.println("AAAA");
}
}
class CCC extends AAA
{
public void test(){
System.out.println("CCC");
}
}