黑马程序员 Java高级特性II

---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------

 

编程实例:注解

package cn.heima.day2;

import java.text.Annotation;

@ItcastAnnotation
public class AnnotationTest {

 /**
  * @param args
  */
 @SuppressWarnings("deprecation")
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.runFinalizersOnExit(true);
  
  AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class);
  ItcastAnnotation annotation = AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
  System.out.println(annotation);
 }
 
 @Deprecated
 public static void sayHello(){
  System.out.println("hi");
 }

}

package cn.heima.day2;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/*元注解
元数据
元信息*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ItcastAnnotation {

}


编程实例:类加载

package cn.heima.day2;

public class ClassLoaderTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println(
    ClassLoaderTest.class.getClassLoader().getClass()
    .getName()
    );
  
  System.out.println(
    System.class.getClassLoader()
    );
  
  ClassLoader loader = ClassLoader.class.getClassLoader();
  while(loader != null){
   System.out.println(loader.getClass().getName());
   loader = loader.getParent();
  }
  System.out.println(loader);
 }

}

编程实例:泛型测试

package cn.heima.day2;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Vector;

import cn.heima.day1.ReflectPoint;

public class GenericTest {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception {
  
  GenericDao<ReflectPoint> dao = new GenericDao<ReflectPoint>();
  dao.add(new ReflectPoint(3, 3));
  
  Vector <Date> v = new Vector <Date>();
  Method applyMethod = GenericTest.class.getMethod("applyVector", Vector.class);
  Type[] types  = applyMethod.getGenericParameterTypes();
  ParameterizedType pType = (ParameterizedType)types[0];
  System.out.println(pType.getRawType());
  System.out.println(pType.getActualTypeArguments()[0]);
 }
 
public static void applyVector(Vector<Date> v){
 
}
}

package cn.heima.day2;

import java.util.Set;

public class GenericDao <T>{
 
 public void add(T x){
  
 }
 
 public  T findById(int id){
  return null;
 }
 
 public void delete(T obj){
  
 }
 
 public void delete(int id){
  
 }
 
 public void update(T obj){
  
 }
 
 public T findByUserName(String name){
  return null;
 }
 
 public Set<T> findByConditions(String where){
  return null;
 }
 
 
}

编程实例:静态测试

package cn.heima.day2;

public class StaticTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Employee[] staff = new Employee[3];
  staff[0] = new Employee("Tom", 40000);
  staff[1] = new Employee("Dark", 50000);
  staff[2] = new Employee("Harry", 60000);
  
  for(Employee e : staff){
   e.setId(0);
   System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" + e.getSalary());
  }
  
  int n = Employee.getNextId();
  System.out.println("Next available id=" +n);
 }

}

编程实例:JavaBean测试

package cn.heima.day2;

public class Employee {

 /**
  * @param args
  */
 public Employee(String n, double s){
  name = n;
     salary = s;
  id = 0;
 }
 
 
 public int getId() {
  return id;
 }


 public void setId(int id) {
  id = nextId;
  nextId++;
 }


 public String getName() {
  return name;
 }


 public double getSalary() {
  return salary;
 }


 public static int getNextId() {
  return nextId;
 }


 public static void main(String[] args) {
  Employee e = new Employee("harry", 5000);
  System.out.println(e.getName()+ " " +e.getSalary());
 }
 
 private String name;
 private double salary;
 private int id;
 private static int nextId = 1;

}

 ---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值