Java 接口,继承 用法意外体会。
2011-12-26 18:24
211 查看
一个类只可以继承一个类,
一个接口却可以继承多个接口。
父类的方法,子类继承后如果重写。则创建子类对象后使用该方法调用的是子类中的该方法。 否则,即没有修改父类里的方法,调用的就仍是父类方法。
package 复习;
class Base{
void f(){System.out.println("调用的是父类里的方法!");}
}
public class Test extends Base {
public void f(){
//super.f();
System.out.println("调用的是子类里的方法!");
}
public static void main(String[] args) {
Base b1=new Test();
Test t1=new Test();
b1.f();
t1.f();
}
}
调用的是子类里的方法!
调用的是子类里的方法!
package 复习;
class Base{
void f(){System.out.println("调用的是父类里的方法!");}
}
public class Test extends Base {
public void f(){
//super.f();
System.out.println("调用的是子类里的方法!");
}
public static void main(String[] args) {
Base b1=new Base();
Test t1=new Test();
b1.f();
t1.f();
}
}
调用的是父类里的方法!
调用的是子类里的方法!
一个接口可以继承多个接口,最终某类再执行该接口(集成了多个接口的接口)时,必须实例化所有抽象函数。 当然,这个类同时也可以继承一个父类,倘若该父类中的 方法 shortage()在接口AsianMan中已有,这个子类依然要实例化shortage()方法,且该子类对象在调用时使用的是接口中的shortage()!!!
package interfaces;
interface Animal{
void eat();
}
interface Human{
void drink();
}
interface Man{
void run();
}
interface AsianMan extends Animal,Human,Man{
void shortage();
}
class HeNanMan{
void shortage(){
System.out.println("But he was slightly inferior, especially in matters of taste and style .");
}
}
class ME extends HeNanMan implements AsianMan{
public void eat(){System.out.print("This type can eat, ");}
public void drink(){System.out.print("can drink,");}
public void run(){System.out.print(" can run.");}
public void shortage(){System.out.println("But he has few ambition !!!");}
}
public class TryInterface {
public static void main(String[] args) {
ME fc=new ME();
fc.eat();
fc.drink();
fc.run();
fc.shortage();
//fc.shortage();
}
}
output: This type can eat, can drink, can run.But he has few ambition !!!
假使没有冲突,那都可以调用。
package interfaces;
interface Animal{
void eat();
}
interface Human{
void drink();
}
interface Man{
void run();
}
interface AsianMan extends Animal,Human,Man{
void shortage();
}
class HeNanMan{
void shortage2(){
System.out.println(" and he was slightly inferior, especially in matters of taste and style .");
}
}
class ME extends HeNanMan implements AsianMan{
public void eat(){System.out.print("This type can eat, ");}
public void drink(){System.out.print("can drink,");}
public void run(){System.out.print(" can run.");}
public void shortage(){System.out.print("But he has few ambition !!!");}
}
public class TryInterface {
public static void main(String[] args) {
ME fc=new ME();
fc.eat();
fc.drink();
fc.run();
fc.shortage();
fc.shortage2();
}
}
output: This type can eat, can drink, can run.But he has few ambition !!! and he was slightly inferior, especially in matters of taste and style .
接口可以选择性继承,只执行一部分抽象方法
package interfaces;
interface Animal{
void eat();
}
interface Human{
void drink();
}
interface Man{
void run();
}
interface AsianMan extends Animal,Human{
void shortage();
}
class HeNanMan{
void run(){System.out.print("can run more faster than other province. ");}
void shortage2(){
System.out.println("and he was slightly inferior, especially in matters of taste and style .");
}
}
class ME extends HeNanMan implements AsianMan{
public void eat(){System.out.print("This type can eat, ");}
public void drink(){System.out.print("can drink,");}
//public void run(){System.out.print(" can run.");}
public void shortage(){System.out.print("But he has few ambition !!!");}
}
public class TryInterface {
public static void main(String[] args) {
ME fc=new ME();
fc.eat();
fc.drink();
fc.run();
fc.shortage();
fc.shortage2();
}
}
output: This type can eat, can drink,can run more faster than other province. But he has few ambition !!!and he was slightly inferior, especially in matters of taste and style .
一个接口却可以继承多个接口。
父类的方法,子类继承后如果重写。则创建子类对象后使用该方法调用的是子类中的该方法。 否则,即没有修改父类里的方法,调用的就仍是父类方法。
package 复习;
class Base{
void f(){System.out.println("调用的是父类里的方法!");}
}
public class Test extends Base {
public void f(){
//super.f();
System.out.println("调用的是子类里的方法!");
}
public static void main(String[] args) {
Base b1=new Test();
Test t1=new Test();
b1.f();
t1.f();
}
}
调用的是子类里的方法!
调用的是子类里的方法!
package 复习;
class Base{
void f(){System.out.println("调用的是父类里的方法!");}
}
public class Test extends Base {
public void f(){
//super.f();
System.out.println("调用的是子类里的方法!");
}
public static void main(String[] args) {
Base b1=new Base();
Test t1=new Test();
b1.f();
t1.f();
}
}
调用的是父类里的方法!
调用的是子类里的方法!
一个接口可以继承多个接口,最终某类再执行该接口(集成了多个接口的接口)时,必须实例化所有抽象函数。 当然,这个类同时也可以继承一个父类,倘若该父类中的 方法 shortage()在接口AsianMan中已有,这个子类依然要实例化shortage()方法,且该子类对象在调用时使用的是接口中的shortage()!!!
package interfaces;
interface Animal{
void eat();
}
interface Human{
void drink();
}
interface Man{
void run();
}
interface AsianMan extends Animal,Human,Man{
void shortage();
}
class HeNanMan{
void shortage(){
System.out.println("But he was slightly inferior, especially in matters of taste and style .");
}
}
class ME extends HeNanMan implements AsianMan{
public void eat(){System.out.print("This type can eat, ");}
public void drink(){System.out.print("can drink,");}
public void run(){System.out.print(" can run.");}
public void shortage(){System.out.println("But he has few ambition !!!");}
}
public class TryInterface {
public static void main(String[] args) {
ME fc=new ME();
fc.eat();
fc.drink();
fc.run();
fc.shortage();
//fc.shortage();
}
}
output: This type can eat, can drink, can run.But he has few ambition !!!
假使没有冲突,那都可以调用。
package interfaces;
interface Animal{
void eat();
}
interface Human{
void drink();
}
interface Man{
void run();
}
interface AsianMan extends Animal,Human,Man{
void shortage();
}
class HeNanMan{
void shortage2(){
System.out.println(" and he was slightly inferior, especially in matters of taste and style .");
}
}
class ME extends HeNanMan implements AsianMan{
public void eat(){System.out.print("This type can eat, ");}
public void drink(){System.out.print("can drink,");}
public void run(){System.out.print(" can run.");}
public void shortage(){System.out.print("But he has few ambition !!!");}
}
public class TryInterface {
public static void main(String[] args) {
ME fc=new ME();
fc.eat();
fc.drink();
fc.run();
fc.shortage();
fc.shortage2();
}
}
output: This type can eat, can drink, can run.But he has few ambition !!! and he was slightly inferior, especially in matters of taste and style .
接口可以选择性继承,只执行一部分抽象方法
package interfaces;
interface Animal{
void eat();
}
interface Human{
void drink();
}
interface Man{
void run();
}
interface AsianMan extends Animal,Human{
void shortage();
}
class HeNanMan{
void run(){System.out.print("can run more faster than other province. ");}
void shortage2(){
System.out.println("and he was slightly inferior, especially in matters of taste and style .");
}
}
class ME extends HeNanMan implements AsianMan{
public void eat(){System.out.print("This type can eat, ");}
public void drink(){System.out.print("can drink,");}
//public void run(){System.out.print(" can run.");}
public void shortage(){System.out.print("But he has few ambition !!!");}
}
public class TryInterface {
public static void main(String[] args) {
ME fc=new ME();
fc.eat();
fc.drink();
fc.run();
fc.shortage();
fc.shortage2();
}
}
output: This type can eat, can drink,can run more faster than other province. But he has few ambition !!!and he was slightly inferior, especially in matters of taste and style .
相关文章推荐
- 【Java学习笔记之二十二】解析接口在Java继承中的用法及实例分析
- 【Java学习笔记之二十二】解析接口在Java继承中的用法及实例分析
- 黑马程序员——Java基础:继承、final关键字、抽象、接口、多态
- JAVA学习心得--继承、接口与泛型
- java——构造函数、构造代码块、Static关键字、main函数 & 继承:子父类中的变量、函数的特点、抽象类、接口等
- Java并发03:多线程实现三方式:继承Thread类、实现Runnable接口、实现Callable接口
- 实验3_Java类的继承和接口的定义和使用
- 一次性理清java 中的 类、抽象类、接口、继承、对象
- java接口继承与多态
- JAVA 继承基本类、抽象类、接口
- 黑马程序员——java基础之继承、抽象、接口
- java中Comparator接口的用法
- 第五章 继承、接口与泛型 分类: java 2009-12-29 16:43 348人阅读 评论(0) 收藏
- 黑马程序员-自己总结的java学习笔记(5)继承,接口,多态。
- [Java之二]继承与接口
- [原创]Java中使用File类的list方法获取一定数量的文件:FilenameFilter接口的特殊用法
- java用接口、多态、继承、类计算三角形和矩形周长及面积的方法
- 黑马程序员——Java基础---继承,抽象,多态,接口,包,内部类
- JAVA 内部类、外部类、继承、抽象类、接口实战
- Mono 继承一些特殊接口中带有 IJavaObject, IDisposable 接口的实现方法