黑马程序员 Java高级特性I

 

---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------
编写程序: 自动装箱

package cn.edu.heima;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class TestProperties {

 /**
  * @param args
  * @throws IOException
  * @throws InterruptedException
  */
 public static void main(String[] args) throws IOException, InterruptedException {
  // TODO Auto-generated method stub
  Properties sp = System.getProperties();
  Enumeration e = sp.propertyNames();
  while(e.hasMoreElements())
  {
   String key = (String)e.nextElement();
   System.out.println(key + "=" + sp.getProperty(key));
   
  }
  Process p = null;
  try{
  p = Runtime.getRuntime().exec("notepad.exe API.java");
  Thread.sleep(50000);
  p.destroy();
  }
  catch(Exception q){}
 }

}

编程实例: 枚举测试

package cn.heima.day1;

public class EnumTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  WeekDay weekDay = WeekDay.SUN;
  System.out.println(weekDay);
  
  WeekDay weekDay1 = WeekDay.FRI;
  System.out.println(weekDay1);
  System.out.println(weekDay1.ordinal());
  System.out.println(weekDay1.name());
  System.out.println(weekDay1.getClass());
  System.out.println(WeekDay.valueOf("SUN"));
  System.out.println(WeekDay.values());
  System.out.println(WeekDay.valueOf("SUN").toString());
  System.out.println(WeekDay.values().length);
 }
 public enum WeekDay{SUN,MON,TUE,WED,THI,FRI,SAT;

 public char[] compareTo() {
  // TODO Auto-generated method stub
  return null;
 }}

}

编程实例: 内省

package cn.heima.day1;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ItroSpectorTest {

 /**
  * @param args
  * @throws IntrospectionException
  * @throws InvocationTargetException
  * @throws IllegalAccessException
  * @throws IllegalArgumentException
  */
 public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
  
  ReflectPoint pt1 = new ReflectPoint(3,5);
  
  String propertyName = "x";
  
  Object retVal = getProperty(pt1, propertyName);
  System.out.println(retVal);
  
  Object value = 7;
  setProperties(pt1, propertyName, value);
  System.out.println(pt1.getX());
 }

 private static void setProperties(ReflectPoint pt1, String propertyName,
   Object value) throws IntrospectionException,
   IllegalAccessException, InvocationTargetException {
  PropertyDescriptor  pd2 = new PropertyDescriptor(propertyName,pt1.getClass());
  Method methodSetX = pd2.getWriteMethod();
     methodSetX.invoke(pt1,value);
 }

 private static Object getProperty(ReflectPoint pt1, String propertyName)
   throws IntrospectionException, IllegalAccessException,
   InvocationTargetException {
  /*PropertyDescriptor  pd = new PropertyDescriptor(propertyName,pt1.getClass());
  Method methodGetX = pd.getReadMethod();
  Object retVal = methodGetX.invoke(pt1);
  return retVal;*/
  
  BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
  PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
  Object retVal = null;
  for(PropertyDescriptor pd : pds){
   if(pd.getName().equals(propertyName)){
    
    Method methodGetX = pd.getReadMethod();
    retVal = methodGetX.invoke(pt1);
    break;
   }
  }
  return retVal;
 }

}

package cn.heima.day1;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ItroSpectorTest {

 /**
  * @param args
  * @throws IntrospectionException
  * @throws InvocationTargetException
  * @throws IllegalAccessException
  * @throws IllegalArgumentException
  */
 public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
  
  ReflectPoint pt1 = new ReflectPoint(3,5);
  
  String propertyName = "x";
  
  Object retVal = getProperty(pt1, propertyName);
  System.out.println(retVal);
  
  Object value = 7;
  setProperties(pt1, propertyName, value);
  System.out.println(pt1.getX());
 }

 private static void setProperties(ReflectPoint pt1, String propertyName,
   Object value) throws IntrospectionException,
   IllegalAccessException, InvocationTargetException {
  PropertyDescriptor  pd2 = new PropertyDescriptor(propertyName,pt1.getClass());
  Method methodSetX = pd2.getWriteMethod();
     methodSetX.invoke(pt1,value);
 }

 private static Object getProperty(ReflectPoint pt1, String propertyName)
   throws IntrospectionException, IllegalAccessException,
   InvocationTargetException {
  /*PropertyDescriptor  pd = new PropertyDescriptor(propertyName,pt1.getClass());
  Method methodGetX = pd.getReadMethod();
  Object retVal = methodGetX.invoke(pt1);
  return retVal;*/
  
  BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
  PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
  Object retVal = null;
  for(PropertyDescriptor pd : pds){
   if(pd.getName().equals(propertyName)){
    
    Method methodGetX = pd.getReadMethod();
    retVal = methodGetX.invoke(pt1);
    break;
   }
  }
  return retVal;
 }

}

 

编程实例:反射测试

package cn.heima.day1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;

public class ReflectTest2 {

 /**
  * @param args
  * @throws IOException
  * @throws ClassNotFoundException
  * @throws IllegalAccessException
  * @throws InstantiationException
  */
 public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
  
  InputStream ips = new FileInputStream("config.property");
  Properties pros = new Properties();
  pros.load(ips);
  ips.close();
  String className = pros.getProperty("className");
  Collection collections = (Collection)Class.forName(className).newInstance();
  
  //Collection collections = new HashSet();
  ReflectPoint pt1 = new ReflectPoint(3,3);
  ReflectPoint pt2 = new ReflectPoint(5,5);
  ReflectPoint pt3 = new ReflectPoint(3,3);
  
  collections.add(pt1);
  collections.add(pt2);
  collections.add(pt3);
  collections.add(pt1);
  
  //pt1.y = 7;
  //collections.remove(pt1);
  
  System.out.println(collections.size());
  
  
 }

}

package cn.heima.day1;

public class ReflectPoint {
 
 private int x;
 public int y;
 
 public String str1 = "ball";
 public String str2 = "basketball";
 public String str3 = "cat";
 
 
 public ReflectPoint(int x, int y) {
  super();
  this.x = x;
  this.y = y;
 }
 
 
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + x;
  result = prime * result + y;
  return result;
 }


 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  final ReflectPoint other = (ReflectPoint) obj;
  if (str3 == null) {
   if (other.str3 != null)
    return false;
  } else if (!str3.equals(other.str3))
   return false;
  if (x != other.x)
   return false;
  if (y != other.y)
   return false;
  return true;
 }


 @Override
 public String toString(){
  return str1 + ":" + str2 + ":" + str3;
 }


 public int getX() {
  return x;
 }


 public void setX(int x) {
  this.x = x;
 }


 public int getY() {
  return y;
 }


 public void setY(int y) {
  this.y = y;
 }
}

编程实例:可变参数

package cn.heima.day1;

public class VariableParameter {

 /**
  * @param args
  */
 public static void main(String[] args) {
  System.out.println(add(2));
  System.out.println(add(2,3));
  System.out.println(add(2,3,5));
  }
 public static int add(int x, int ...args){
  int sum = x;
  for(int i=0;i<args.length;i++){
   sum += args[i];
  }
  return sum;
  
 } 

}

package cn.heima.day1;

public abstract class WeekDay {
 
 private WeekDay(){};
 
 public final static WeekDay SUN = new WeekDay(){

  @Override
  public WeekDay nextDay() {
   // TODO Auto-generated method stub
   return MON;
  }
  
 };
 public final static WeekDay MON = new WeekDay(){

  @Override
  public WeekDay nextDay() {
   // TODO Auto-generated method stub
   return SUN;
  }};
 
 public abstract WeekDay nextDay();
 
 /*public WeekDay nextDay(){
  if(this == SUN){
   return MON;
  }
  else{return SUN;}
 }*/
 
 public String toString(){
   return this == SUN?"SUN":"MON";
  }
 }

package cn.heima.day1;

import static java.lang.Math.*;

public class StadicImpert1 {
 public static void main(String args[]){
  int x = 1;
    try {
     x++;
    } finally {
     System.out.println("hello");
    }
  System.out.println(x);
  System.out.println(max(3,6));
 }

}

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

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值