By GokiSoft.com|
20:39 01/07/2022|
Java Basic
[Source Code] Tìm hiểu biến và phương thức static + interface trong OOP - C2108L
#Vehicle.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public class Vehicle implements IRunning{
@Override
public void running() {
System.out.println("Vehicle is running");
}
}
#Tiger.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public class Tiger extends Animal{
@Override
public void running() {
System.out.println("Tiger is running");
}
}
#Testing.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
import java.util.ArrayList;
/**
*
* @author Diep.Tran
*/
public class Testing {
public static void main(String[] args) {
//Chuong trinh quan Animal, Tiger -> Animal, Cat -> Animal
//Student
//Vehicle
//Class Object -> Se gom 1 phuong thuc: running() -> hien thi 1 message
//Phan 1: Thiet ke theo yeu cau tren
//Phan 2: Tao 3 mang -> sinh vien, xe co, dong vat
ArrayList<Student> students = new ArrayList<>();
//Them sinh vien vao mang tren
students.add(new Student());
students.add(new Student());
for (Student student : students) {
student.running();
}
ArrayList<Vehicle> vehicles = new ArrayList<>();
vehicles.add(new Vehicle());
for (Vehicle vehicle : vehicles) {
vehicle.running();
}
ArrayList<Animal> animals = new ArrayList<>();
animals.add(new Tiger());
animals.add(new Tiger());
animals.add(new Cat());
for (Animal animal : animals) {
animal.running();
}
//Yeu cau: Tao 1 mang -> Quan ly Tiger, Cat, Student, Vehicle
//Interface -> Class -> nhom cac hanh dong chung cua
//cac loai Class Object khac nhau
ArrayList<IRunning> list = new ArrayList<>();
list.add(new Student());
list.add(new Tiger());
list.add(new Cat());
list.add(new Vehicle());
for (IRunning r : list) {
r.running();
}
//1. Interface la gi???
//2. Bien trong interface
// IRunning.x = 100;
System.out.println("x = " + IRunning.x);
//3. Interface co ke thua dc khong?
//Co cay hoi j cho interface
}
}
#Test2.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public class Test2 {
public static void main(String[] args) {
A a = new A();
System.out.println("x = " + a.x);
B b = new B();
System.out.println("x = " + b.x);
A ab = new B();
System.out.println("x = " + ab.x);
}
}
#Student.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public class Student implements IRunning{
@Override
public void running() {
System.out.println("Student is running");
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
import javax.xml.transform.Source;
/**
*
* @author Diep.Tran
*/
public class Main {
//Tim hieu static (bien + methods)
//Interface trong OOP
//Class Object | Class -> Main, Config, ...
//Object -> Main m = new Main() -> m : Object
public static void main(String[] args) {
System.out.println("Host: " + Config.HOST);
Config config = new Config();
System.out.println(config.x);
config.showMsg("12312312");
config.testing("23132"); //warning -> sai ban chat -> chay binh thuong
Config.testing("sdsadsad");
}
}
#IStop.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public interface IStop {
void stop();
void stopping();
}
#IRunning.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public interface IRunning {
int x = 10;//public final
void running();
}
#IAction.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public interface IAction extends IRunning, IStop{
void sleeping();
}
#Config.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public class Config {
public static String HOST = "localhost";
public static String DATABASE = "C2108L";
public static String USERNAME = "root";
public static String PASSWORD = "";
public int x = 10;
public void showMsg(String msg) {
System.out.println("Thong tin: " + msg);
}
public static void testing(String msg) {
System.out.println("OKOK > " + msg);
}
}
#Cat.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public class Cat extends Animal{
@Override
public void running() {
System.out.println("Cat is running");
}
}
#B.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public class B extends A{
public int x = 15;
}
#Animal.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public class Animal implements IRunning{
@Override
public void running() {
System.out.println("Animal is running");
}
}
#A.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson06;
/**
*
* @author Diep.Tran
*/
public class A {
public int x = 10;
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)