package com.example.demo;
import com.example.Pk2.Line;
import com.example.Pk2.Rectangle;
import com.example.Pk2.Shape;
import org.junit.Test;
public class SwitchTest {
@Test
public void Test01(){
int week = 6;
// 计算结果
String memo = "";
switch (week){
case 1 -> memo = "休息日";
case 2,3,4,5,6 -> memo = "工作日";
case 7 -> memo = "星期六";
default -> throw new RuntimeException("无效的日期");
}
System.out.println(memo);
}
@Test
public void Test02(){
int week = 60;
// 计算结果
String memo = switch (week){
case 1 : yield "休息日";
case 2,3,4,5,6 : yield "工作日";
case 7 : yield "星期六";
default : yield "无效的日期";
};
System.out.println(memo);
}
@Test
public void Test03(){
int week = 1;
String memo = switch (week){
case 1 -> {
System.out.println("星期日");
yield "星期日";
}
case 2,3,4,5,6 -> {
System.out.println("工作日");
yield "工作日";
}
case 7 -> {
System.out.println("星期六");
yield "星期六";
}
default -> {
System.out.println("非法日期");
yield "非法日期";
}
};
System.out.println("memo是"+memo);
}
@Test
public void Test04(){
Line line = new Line(10, 20);
Rectangle rectangle = new Rectangle(20, 50);
Shape shape = new Shape(50, 80);
Object obj = shape;
int result = switch (obj){
case Line(int x,int y) -> {
// 可以在代码块操作我们的Record的值,非常方便
System.out.println("Line");
yield x + y;
}
// 可以在代码块操作我们的Record的值,非常方便
case Rectangle(int w,int h) -> 2 * (2 + h);
// 可以在代码块操作我们的Record的值,非常方便
case Shape(int x,int y) -> {
System.out.println("Shape");
yield x * y;
}
default -> {
System.out.println("啥也不是,散会");
yield 0;
}
};
System.out.println(result);
}
@Test
public void Test(){
String s = """
Jack
Rose
select * from employees
""";
System.out.println(s);
}
@Test
public void Test05(){
// 加了空格也会自动trim掉,仍然结果为true
String s = """
lisi
""";
String s1 = """
lisi
""";
System.out.println(s.equals(s1));
System.out.println(s == s1);
String msg = """
Hello World
""";
System.out.println(msg.substring(0, 5));
}
@Test
public void Test06(){
// 需要按Tab来后推,用空格键会被trim掉
// tab才能保留缩进效果
String html = """
<html>
<body>哈哈哈哈</body>
</html>
""";
System.out.println(html);
}
@Test
public void Test07(){
String colors = """
red
green
blue
""";
// 加五个空格
colors = colors.indent(5);
System.out.println(colors);
}
@Test
public void Test08(){
String info = """
Name:%s,
Phone:%s,
Age:%d
""".formatted("Jack","15919996103",20);
// formatted方法可以为文本块中的内容赋值
System.out.println(info);
}
@Test
public void Test09(){
// \表示换行符,避免了一行文字过长且实现了可读性的增强
String info = """
Spring Boot是一个快速开发框架\
是\"Spring\"家族的一个成员\
创建Spring项目
""";
System.out.println(info);
}
@Test
public void varTest(){
var s1 = "Hello";
var i = 10;
}
}
package com.example.demo;
import com.example.Pk2.Line;
import com.example.Pk2.Rectangle;
import com.example.Pk2.Shape;
import org.junit.Test;
public class SwitchTest {
@Test
public void Test01(){
int week = 6;
// 计算结果
String memo = "";
switch (week){
case 1 -> memo = "休息日";
case 2,3,4,5,6 -> memo = "工作日";
case 7 -> memo = "星期六";
default -> throw new RuntimeException("无效的日期");
}
System.out.println(memo);
}
@Test
public void Test02(){
int week = 60;
// 计算结果
String memo = switch (week){
case 1 : yield "休息日";
case 2,3,4,5,6 : yield "工作日";
case 7 : yield "星期六";
default : yield "无效的日期";
};
System.out.println(memo);
}
@Test
public void Test03(){
int week = 1;
String memo = switch (week){
case 1 -> {
System.out.println("星期日");
yield "星期日";
}
case 2,3,4,5,6 -> {
System.out.println("工作日");
yield "工作日";
}
case 7 -> {
System.out.println("星期六");
yield "星期六";
}
default -> {
System.out.println("非法日期");
yield "非法日期";
}
};
System.out.println("memo是"+memo);
}
@Test
public void Test04(){
Line line = new Line(10, 20);
Rectangle rectangle = new Rectangle(20, 50);
Shape shape = new Shape(50, 80);
Object obj = shape;
int result = switch (obj){
case Line(int x,int y) -> {
// 可以在代码块操作我们的Record的值,非常方便
System.out.println("Line");
yield x + y;
}
// 可以在代码块操作我们的Record的值,非常方便
case Rectangle(int w,int h) -> 2 * (2 + h);
// 可以在代码块操作我们的Record的值,非常方便
case Shape(int x,int y) -> {
System.out.println("Shape");
yield x * y;
}
default -> {
System.out.println("啥也不是,散会");
yield 0;
}
};
System.out.println(result);
}
@Test
public void Test(){
String s = """
Jack
Rose
select * from employees
""";
System.out.println(s);
}
@Test
public void Test05(){
// 加了空格也会自动trim掉,仍然结果为true
String s = """
lisi
""";
String s1 = """
lisi
""";
System.out.println(s.equals(s1));
System.out.println(s == s1);
String msg = """
Hello World
""";
System.out.println(msg.substring(0, 5));
}
@Test
public void Test06(){
// 需要按Tab来后推,用空格键会被trim掉
// tab才能保留缩进效果
String html = """
<html>
<body>哈哈哈哈</body>
</html>
""";
System.out.println(html);
}
@Test
public void Test07(){
String colors = """
red
green
blue
""";
// 加五个空格
colors = colors.indent(5);
System.out.println(colors);
}
@Test
public void Test08(){
String info = """
Name:%s,
Phone:%s,
Age:%d
""".formatted("Jack","15919996103",20);
// formatted方法可以为文本块中的内容赋值
System.out.println(info);
}
@Test
public void Test09(){
// \表示换行符,避免了一行文字过长且实现了可读性的增强
String info = """
Spring Boot是一个快速开发框架\
是\"Spring\"家族的一个成员\
创建Spring项目
""";
System.out.println(info);
}
@Test
public void varTest(){
var s1 = "Hello";
var i = 10;
}
}
package com.example.Pk3;
//密闭类只有指定的可继承
public sealed class Shape permits Circle,Square,Rectangle{
private Integer width;
private Integer height;
public void draw(){
System.out.println(width);
System.out.println(height);
System.out.println(this.getClass().getName());
}
}
package com.example.Pk3;
//密闭类只有指定的可继承
public sealed class Shape permits Circle,Square,Rectangle{
private Integer width;
private Integer height;
public void draw(){
System.out.println(width);
System.out.println(height);
System.out.println(this.getClass().getName());
}
}
package com.example.Pk3;
//放弃密闭,可以任意继承
public non-sealed class Rectangle extends Shape{
@Override
public void draw() {
System.out.println("Rectangle");
}
}
package com.example.Pk3;
//放弃密闭,可以任意继承
public non-sealed class Rectangle extends Shape{
@Override
public void draw() {
System.out.println("Rectangle");
}
}
package com.example.Pk3;
//仍是密闭类,可以指定继承者
public sealed class Square extends Shape permits RoundSquare{
@Override
public void draw() {
System.out.println("Square");
}
}
package com.example.Pk3;
//仍是密闭类,可以指定继承者
public sealed class Square extends Shape permits RoundSquare{
@Override
public void draw() {
System.out.println("Square");
}
}
package com.example.Pk3;
//final修饰的都不可在继承
public final class RoundSquare extends Square {
@Override
public void draw() {
System.out.println("RoundSquare");
}
}
package com.example.Pk3;
//final修饰的都不可在继承
public final class RoundSquare extends Square {
@Override
public void draw() {
System.out.println("RoundSquare");
}
}
package com.example.Pk3;
//final修饰的都不可在继承
public final class Circle extends Shape{
@Override
public void draw() {
System.out.println("Circle");
}
}
package com.example.Pk3;
//final修饰的都不可在继承
public final class Circle extends Shape{
@Override
public void draw() {
System.out.println("Circle");
}
}