Java学习过程(第十天)

/*
interface Usb  //定义一个接口
{
public void open();
public void close();
}
class Upan implements Usb
{
public void open()
{
System.out.println("Upan open");
}
public void close()
{
System.out.println("Upan close");
}
}
class Mouse implements Usb
{
public void open()
{
System.out.println("Mouse open");
}
public void close()
{
System.out.println("Mouse close");
}
}
class InterfaceDemo
{
public static void main(String[] args)
{
show(new Upan());
show(new Mouse());
}
public static void show(Usb u)//接口类型指向子类对象 (多态:父类或者接口的引用指向其子类的对象。)
{
u.open();
u.close();
}
}
//注意static的用法


*/






/*
abstract class animal
{
abstract void eat();//抽象类里面的方法也必须是抽象方法,要用abstract修饰
}
class cat extends animal
{
void eat()
{
System.out.println("cat eat fish");
}
void catchMouse()
{
System.out.println("cat catch mouse");
}
}
class dog extends animal
{
void eat()
{
System.out.println("dog eat meat");
}
void kanjia()
{
System.out.println("dog kanjia");
}
}
class InterfaceDemo
{
public static void main(String[] args)
{
animal p1=new cat();
method(p1);
// cat p2=(cat)p1;
// p2.catchMouse();
//cat p2=new cat();
//p2.catchMouse();
}
public static void method(animal a)  //animal x=new cat();只能调用接口或者父类中的内容
{
a.eat();


if (a instanceof cat)
{
cat b=(cat)a;
b.catchMouse();
}
else if(a instanceof dog)
{
dog c=(dog)a;
c.kanjia();
}
else
{

}
}


}


*/




/*
class animal 
{
void eat()
{
System.out.println("animal eat somthing");
}
}
class dog extends animal
{
void eat()
{
System.out.println("dog eat somthing");
}
void show()
{
System.out.println("hello im cool");
}
}
class InterfaceDemo
{
public static void main(String[] args)
{
//animal a=new dog();//父类类型指向子类对象,调用的是子类的方法;
//a.eat();
animal a=new dog();
dog c=(dog)a;//强制类型转换,就可以调用子类方法了
c.show();
}
}


*/




/*
class Fu
{
static int num=3;
int x=10;
void show()
{
System.out.println("Fu show");
}
static void method()
{
System.out.println("Fu method");
}
}
class Zi extends Fu
{
static int num=4;
int x=11;
void show()
{
System.out.println("Zi show");
}
static void method()
{
System.out.println("Zi method");
}
}


class InterfaceDemo
{
public static void main(String[] args)
{




//Fu.method();
//Zi.method();
Fu p1=new Fu();
System.out.println("num="+p1.num);
System.out.println("x="+p1.x);
p1.show();
p1.method();
/*
num=3
x=10
Fu show
Fu method
*/

Zi p2=new Zi();
System.out.println("num="+p2.num);
System.out.println("x="+p2.x);
p2.show();
p2.method();

/*
num=4
x=11
Zi show
Zi method
*/
Fu p3=new Zi();
System.out.println("num="+p3.num);
System.out.println("x="+p3.x);
p3.show();
p3.method();


/*
num=3
x=10
Zi show
Fu method
*/



/*
综上所述:
调用(静态or非静态)成员变量和静态函数时,输出结果看左边;
调用非静态函数时,输出结果看右边;


}


}


*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值