Spring-Boot框架Record类型和普通Java类一样实现接口并重写方法测试------Spring-Boot框架


  4.0.0

  com.powernode
  Feature
  1.0-SNAPSHOT
  jar

  Feature
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      4.13.2
      test
    
    
      org.projectlombok
      lombok
      1.18.30
    
  
  
    
      
        org.apache.maven.plugins
        maven-compiler-plugin
        3.11.0
        
          17
          17
        
      
    
  


  4.0.0

  com.powernode
  Feature
  1.0-SNAPSHOT
  jar

  Feature
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      4.13.2
      test
    
    
      org.projectlombok
      lombok
      1.18.30
    
  
  
    
      
        org.apache.maven.plugins
        maven-compiler-plugin
        3.11.0
        
          17
          17
        
      
    
  

package com.powernode.Pk1;

public interface PrintInterface {
//    输出自定义的信息
    void print();
}
package com.powernode.Pk1;

public interface PrintInterface {
//    输出自定义的信息
    void print();
}
package com.powernode.Pk1;

import java.util.StringJoiner;

public record ProductRecord(Integer id, String name, Integer count)implements PrintInterface {
    @Override
    public void print() {
        StringJoiner joiner = new StringJoiner("-");
        String s = joiner.add(id.toString()).add(name).add(count.toString()).toString();
        System.out.println(s);
    }
}
package com.powernode.Pk1;

import java.util.StringJoiner;

public record ProductRecord(Integer id, String name, Integer count)implements PrintInterface {
    @Override
    public void print() {
        StringJoiner joiner = new StringJoiner("-");
        String s = joiner.add(id.toString()).add(name).add(count.toString()).toString();
        System.out.println(s);
    }
}
package com.powernode.Pk1;

import java.util.Optional;

public record Student(Integer id, String name, String email, Integer age) {
    public String concat(){
        return String.format(this.name+this.age);
    }
    public static String emailToUpperCase(String email){
        return Optional.ofNullable(email).orElse("no Email").toUpperCase();
    }
//    会执行这个紧凑型构造方法
    public Student{
        System.out.println(id);
        if(id < 1){
            throw new RuntimeException("id小于1");
        }
    }
//    再执行这个构造方法
    public Student(Integer id,String name){
        this(id,name,null,null);
    }
}
package com.powernode.Pk1;

import java.util.Optional;

public record Student(Integer id, String name, String email, Integer age) {
    public String concat(){
        return String.format(this.name+this.age);
    }
    public static String emailToUpperCase(String email){
        return Optional.ofNullable(email).orElse("no Email").toUpperCase();
    }
//    会执行这个紧凑型构造方法
    public Student{
        System.out.println(id);
        if(id < 1){
            throw new RuntimeException("id小于1");
        }
    }
//    再执行这个构造方法
    public Student(Integer id,String name){
        this(id,name,null,null);
    }
}
package com.powernode;

import com.powernode.Pk1.ProductRecord;
import com.powernode.Pk1.Student;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class StudentTest {
    @Test
    public void Test01(){
//        创建Record对象
//        这里也会先执行紧凑的构造方法
        Student Rose = new Student(1001,"Rose","abc",15);
        System.out.println(Rose.toString());
        Integer age = Rose.age();
        System.out.println(age);
        String name = Rose.name();
        System.out.println(name);
        String email = Rose.email();
        System.out.println(email);
        System.out.println("哈哈");
        System.out.println(Rose.concat());
        System.out.println(Student.emailToUpperCase("Dasd"));
        Student jack = new Student(2001, "Jack");
        System.out.println(jack);
    }
    @Test
    public void Test02(){
        ProductRecord record = new ProductRecord(1,"手机",200);
        record.print();
    }
}
package com.powernode;

import com.powernode.Pk1.ProductRecord;
import com.powernode.Pk1.Student;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class StudentTest {
    @Test
    public void Test01(){
//        创建Record对象
//        这里也会先执行紧凑的构造方法
        Student Rose = new Student(1001,"Rose","abc",15);
        System.out.println(Rose.toString());
        Integer age = Rose.age();
        System.out.println(age);
        String name = Rose.name();
        System.out.println(name);
        String email = Rose.email();
        System.out.println(email);
        System.out.println("哈哈");
        System.out.println(Rose.concat());
        System.out.println(Student.emailToUpperCase("Dasd"));
        Student jack = new Student(2001, "Jack");
        System.out.println(jack);
    }
    @Test
    public void Test02(){
        ProductRecord record = new ProductRecord(1,"手机",200);
        record.print();
    }
}

你可能感兴趣的:(JAVA,#,Spring框架,#,Spring-Boot框架,java,spring,boot,spring,后端,maven,java-ee,junit)