By GokiSoft.com|
15:01 21/09/2022|
Java Basic
[Source Code] Tìm hiểu String & StringBuilder & StringBuffer - Lambda - Java - C2109I
#IAction.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package com.gokisoft.lesson06;
/**
*
* @author diepvan
*/
public interface IAction {
void sleeping();
void running();
}
#IMessage.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package com.gokisoft.lesson06;
/**
*
* @author diepvan
*/
@FunctionalInterface
public interface IMessage {
void showMsg(String msg);
}
#IRunning.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson06;
/**
*
* @author diepvan
*/
public interface IRunning {
void onRunning();
}
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson06;
/**
*
* @author diepvan
*/
public class Main {
public static void main(String[] args) throws Exception {
String str = "Aptech 285 Doi Can";
// String s1 = new String("Xin Chao");
// str += "asd";
StringBuilder builder = new StringBuilder();
builder.append("Vi du 1");
builder.append("Vi du 2");
builder.append("Vi du 3");
builder.append("Vi du 4");
builder.append("Vi du 5");
System.out.println(builder.toString());
StringBuffer buffer = new StringBuffer();
buffer.append("ABC");
buffer.append("ABC");
buffer.append("ABC");
buffer.append("ABC");
buffer.append("ABC");
System.out.println(buffer.toString());
int x = 5, y = 0;
try {
int z = x/y;
System.out.println("z = " + z);
} catch(Exception e) {
e.printStackTrace();
}
throw new Exception("asdsadad");
}
}
#People.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson06;
/**
*
* @author diepvan
*/
public class People implements IRunning{
@Override
public void onRunning() {
System.out.println("People is running ...");
}
}
#Test.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson06;
/**
*
* @author diepvan
*/
public class Test {
public static void main(String[] args) {
People p = new People();
p.onRunning();
IRunning r = new IRunning() {
@Override
public void onRunning() {
System.out.println("S is running");
}
};
r.onRunning();
IAction a = new IAction() {
@Override
public void sleeping() {
System.out.println("Sleeping ...");
}
@Override
public void running() {
System.out.println("Running ...");
}
};
a.running();
a.sleeping();
//Lambda
IRunning r1 = () -> {
System.out.println("S is running");
};
r1.onRunning();
IRunning r2 = () -> System.out.println("S is running");
r2.onRunning();
IMessage m = (String msg) -> {
System.out.println("Hello > " + msg);
};
m.showMsg("ABC");
IMessage m2 = (msg) -> {
System.out.println("Hello > " + msg);
};
m2.showMsg("ABC");
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)