2021.09.29
- 资源绑定器和类加载器:
- 可变长参数以及通过反射机制调用对象方法:
- 牛客网专项练习01:多态,线程安全,后台线程
- 牛客网专项练习02:线程抢占时间片,多态,值传递,包装类
- 自定义注解以及反射注解:
- 注解在开发中有什么用呢:注解经典案例
资源绑定器和类加载器:
package du;
import java.util.ResourceBundle;
public class d819 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("du/classinfo");
String className = bundle.getString("className");
System.out.println(className); // 得到里面的东西 du/Product
}
}
可变长参数以及通过反射机制调用对象方法:
package du;
import java.lang.reflect.Method;
public class d827 {
public static void main(String[] args) throws Exception{
m1("123","abc","456","def");
Class> aClass = Class.forName("du.UserService");
Object o = aClass.newInstance();
Method aClassDeclaredMethodm2 = aClass.getDeclaredMethod("m2", String.class);
Object hello_world = aClassDeclaredMethodm2.invoke(o, "hello world");
System.out.println(hello_world); // null;
}
public static void m1(String... args){
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
牛客网专项练习01:多态,线程安全,后台线程
package du;
public class d999 {
public static void main(String[] args) {
byte y = 10;
// byte x = y + y;
// String string = new Object();
}
}
牛客网专项练习02:线程抢占时间片,多态,值传递,包装类
package du;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
public class d999 {
public static void main(String[] args) {
PassO p = new PassO();
p.start();
base base = new Son();
base.method();
// base.methodB(); // 编译不通过
new SuperTest().test();
base1 base1 = new Derived();
base1.methodOne();
final NameList sl = new NameList();
for (int i = 0; i < 2; i++) {
new Thread(){
public void run() {
sl.add("A");
sl.add("B");
sl.add("C");
sl.printAll();
}
}.start();
}
}
}
class Two {
Byte x;
}
class PassO {
void start() {
Two t = new Two();
System.out.print(t.x + ""); // null
Two t2 = fix(t);
System.out.print(t.x + " " + t2.x); // 42 42
}
Two fix(Two tt) {
tt.x = 42;
return tt;
}
}
class base {
public void method() {
System.out.println("base");
}
}
class Son extends base {
public void method() {
System.out.println("Son");
}
public void methodB() {
System.out.println("SonB"); // 编译时出错
}
}
class SuperTest extends Date {
private static final long serialVersionUID = 1L;
public void test() {
System.out.println(super.getClass().getName()); // test.SuperClass
}
}
class base1 {
public void methodOne() {
System.out.print("A"); // 1
methodTwo(); // 调用子类的methodTwo
}
public void methodTwo() {
System.out.print("B"); // 2
}
}
class Derived extends base1 {
public void methodOne() {
super.methodOne(); // 调用父类的methodOne
System.out.print("C"); // 4
}
public void methodTwo() {
super.methodTwo(); // 调用父类的methodTwo
System.out.print("D"); // 3
}
}
class NameList {
private List names = new ArrayList();
synchronized void add(String name) {
names.add(name);
}
synchronized void printAll() {
for (int i = 0; i < names.size(); i++) {
System.out.print(names.get(i) + "");
}
}
}
自定义注解以及反射注解:
// MyAnnotation.java
package du;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "value";
String information();
}
package du;
@MyAnnotation(information = "I am Tom cat")
public class d842 {
private boolean flag;
public static void main(String[] args) throws ClassNotFoundException{
Class> aClass = Class.forName("du.d842");
// System.out.println(aClass.isAnnotationPresent(MyAnnotation.class));
if (aClass.isAnnotationPresent(MyAnnotation.class)){
MyAnnotation annotation = aClass.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
System.out.println(annotation.information());
}
}
}
注解在开发中有什么用呢:注解经典案例
// Id.java
package du;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Id {}
// IdPropertyNotFoundException.java
package du;
public class IdPropertyNotFoundException extends RuntimeException{
public IdPropertyNotFoundException(){}
public IdPropertyNotFoundException(String string){
super(string);
}
}
package du;
import java.lang.reflect.Field;
@Id
public class d844 {
// int id;
String information;
String methods;
public static void main(String[] args) throws ClassNotFoundException{
Class> aClass = Class.forName("du.d844");
if (aClass.isAnnotationPresent(Id.class)){
Field[] declaredFields = aClass.getDeclaredFields();
boolean haveIntId = false;
for (Field declaredField : declaredFields) {
if ("id".equals(declaredField.getName()) && "int".equals(declaredField.getType().getSimpleName())){
haveIntId = true;
break;
}
}
if (!haveIntId){
throw new IdPropertyNotFoundException("Annotation @Id must have int type property id");
}
}
}
}