By GokiSoft.com|
13:42 08/04/2020|
Java Basic
Share Code - Bài học Lambda
Kiến thức hôm nay
- Lambda
- Tìm hiểu kỹ hơn về class object
- Design Pattern
- Chữa bài tập + overview
=========================================
1. Tìm hiểu kỹ hơn về class object
- Tạo 1 abstract class, 1 class, Interface
=> Interface : IRunning
=> Abstract class : Animal
=> Class Object : Dog, Cat
- Cu phap thong thuong * cu phap lambda
=> So luong code ???
=> Thang nao kho hieu ???
- Cu phap lambda duoc ung dung trong cac TH sau
=> Khi khoi tao object tu class object, abstract class, interface
=> can phai dam bao dc yeu cau sau
/*
* 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 lession10;
/**
*
* @author Diep.Tran
*/
public abstract class Animal implements IRunning{
String name, foodType;
public Animal() {
}
public Animal(String name, String foodType) {
this.name = name;
this.foodType = foodType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFoodType() {
return foodType;
}
public void setFoodType(String foodType) {
this.foodType = foodType;
}
public abstract void showSound();
@Override
public void onRunning() {
System.out.println("Animal is running");
}
}
/*
* 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 lession10;
/**
*
* @author Diep.Tran
*/
public class Cat extends Animal{
@Override
public void showSound() {
System.out.println("Meo ... meo ...");
}
}
/*
* 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 lession10;
/**
*
* @author Diep.Tran
*/
public class Dog extends Animal{
@Override
public void showSound() {
System.out.println("Go ... go ...");
}
public void test() {
System.out.println("Testing...");
}
}
/*
* 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 lession10;
/**
*
* @author Diep.Tran
*/
public interface IAction {
void onRunning();
void onSleeping();
}
/*
* 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 lession10;
/**
*
* @author Diep.Tran
*/
public interface IMessage {
void onMessage(String msg);
}
/*
* 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 lession10;
/**
*
* @author Diep.Tran
*/
@FunctionalInterface
public interface IRunning {
void onRunning();
}
/*
* 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 lession10;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
//Khong the khoi tao dc object tu interface hoac abstract class
// Animal animal = new Animal();
// IRunning running = new IRunning();
//Khoi tao dc object tu class object => Kha quen thuoc
Dog dog = new Dog() {
@Override
public void onRunning() {
System.out.println("New onRunning for Dog");
}
};
dog.showSound();
Cat cat = new Cat();
Cat cat1 = new Cat();
Cat cat2 = new Cat();
Cat cat3 = new Cat();
cat.onRunning();
Animal animal = new Dog();
//Khoi tao 1 doi Tiger => abstract Animal
Animal tiger = new Animal() {
@Override
public void showSound() {
System.out.println("Humh ... humh ...");
}
};
tiger.showSound();
Animal tiger1 = new Animal() {
@Override
public void showSound() {
System.out.println("Humh ... humh ...");
}
};
Animal tiger2 = new Animal() {
@Override
public void showSound() {
System.out.println("Humh ... humh ...");
}
};
Animal tiger3 = new Animal() {
@Override
public void showSound() {
System.out.println("Humh ... humh ...");
}
};
Animal dog2 = new Animal() {
@Override
public void showSound() {
System.out.println("Go ... Go ...");
test();
}
void test() {
System.out.println("OKOKOKOK");
}
};//No Name
dog2.showSound();
IRunning r = new IRunning() {
@Override
public void onRunning() {
System.out.println("Test onRunning ...");
}
};
r.onRunning();
//Tom tat
//Biet dc cac cach khac nhau khoi tao 1 object tu class object, abstract class, interface
//Cu phap lambda la gi???
IRunning r1 = () -> {
System.out.println("Test lambda .....");
};
IRunning r2 = () -> System.out.println("Test lambda 2 .....");
r1.onRunning();
r2.onRunning();
IAction action = new IAction() {
@Override
public void onRunning() {
System.out.println("Test running");
}
@Override
public void onSleeping() {
System.out.println("Test sleeping");
}
};
action.onRunning();
action.onSleeping();
//chu y : khi nao su dung dc Lambda Expression => interface
IRunning r3 = () -> {
System.out.println("Test1 .......");
System.out.println("Test2 .......");
};
//TH => Interface phuong thuc co chua tham so
IMessage m = (String msg) -> {
System.out.println("show message >> " + msg);
};
m.onMessage("123");
IMessage m1 = (msg) -> {
System.out.println("show message >> " + msg);
};
m1.onMessage("123");
IMessage m2 = (msg) -> System.out.println("show message >> " + msg);
m2.onMessage("123");
//Hanh vi code
List<Integer> list = new ArrayList<>();
list.add(12);
list.add(122);
list.add(2);
for (int i = 0; i < list.size(); i++) {
System.out.println("show data >> " + list.get(i));
}
for (Integer v : list) {
System.out.println(v);
}
list.forEach((v) -> {
System.out.println(v);
//than nay => code theo nghiep vu yeu cau
});
System.out.println("=======================");
Stream t = list.stream();
t.forEach(System.out::println);
System.out.println("=======================");
Stream t2 = list.parallelStream();
t2.forEach(System.out::println);
//Class Object
Animal a1 = new Dog();
a1.showSound();
a1.onRunning();
//lam the nao chung ta goi dc test() tu doi tuong a1
if(a1 instanceof Dog) {
((Dog) a1).test();
}
Animal a2 = new Animal() {
@Override
public void showSound() {
System.out.println("am thanh.....");
}
public void hello() {
System.out.println("Helloooo");
}
};//ko ten class object
a2.showSound();
//lam sao goi dc hello tu doi tuong a2
//ko the goi dc hello() tu doi tuong a2
//tot nhat => khoi tao 1 class object moi ke thua tu Animal
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)