27 July 2016

Phân biệt Extends Interface và Implements Interface trong java qua ví dụ

Cách hoạt động của Extends và Implements
Class A implements 'interface' B
Java 2016
package Demo24;

interface B {
 void Game();
}

public class A implements B {

 @Override //Eclipse Shift+Alt+s v
 public void Game() {
  System.out.println("Game Spintires");
 }

 public static void main(String[] args) {
  A a = new A();
  a.Game();
 }
}
Output: Game Spintires
So sánh giữa Extends và Implements

Class A implements 'interface' B,C
Java 2016
package Demo24;

interface B {
 public void Game();
}

interface C {
 public void Display();
}

class A implements B, C {

 @Override //Eclipse Shift+Alt+s v
 public void Game() {
  System.out.println("Game Spintires");
 }

 @Override
 public void Display() {
  System.out.println("Game Start!");
 }

 public static void main(String[] args) {
  A a = new A();
  a.Game();
  a.Display();
 }
}
Output: 
Game Spintires
Game Start!
interface D extends interface B,C

class A implements 'interface' D,E
Java 2016
package com.giaima;

interface B {
 void GameAndroid();
}

interface C {
 void GameIOS();
}

interface D extends B, C {
 void GameMoblie();
}

interface E {
 void GamePC();
}

public class A implements D, E {

 @Override // Eclipse Shift+Alt+s v
 public void GameAndroid() {
  System.out.println("GameAndroid");
 }

 @Override
 public void GameIOS() {
  System.out.println("GameIOS");
 }

 @Override
 public void GameMoblie() {
  System.out.println("GameMoblie");
 }
 
 @Override
 public void GamePC() {
  System.out.println("GamePC");
 }

 public static void main(String[] args) {
  A a = new A();
  a.GameAndroid();
  a.GameIOS();
  a.GameMoblie();
  a.GamePC();
 }

}
Output: 
GameAndroid
GameIOS
GameMoblie
GamePC

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang