java基础加强视频学习(—)
?
3.调试技巧:
选择变量,右击选择watch。可以查看变量的值。
4.模板设置
?
5.静态导入:
导入一个类的静态方法。
如:
import static java.lang.Math.*;
6.可变参数:
public static void main(String[] args) { System.out.println(add(1,2,4)); System.out.println(add(1,2,4,5)); } public static int add(int x, int... args){//可变参数 int num=0; for (int i = 0; i < args.length; i++) { num+=args[i]; } return num; }? for (int i : args) { num+=i; }?public static void main(String[] args) { Integer integer1 =12; Integer integer2=12; System.out.println(integer2==integer1); }返回true public static void main(String[] args) { Integer integer1 =150; Integer integer2=150; System.out.println(integer2==integer1); }?返回falsepublic class Static { public static void main(String[] args) { Weekday sun=Weekday.sun; System.out.println(sun.toString()); }}class Weekday{ private Weekday(){} public final static Weekday sun=new Weekday(); public final static Weekday mon=new Weekday(); public Weekday netxDay() { if (this==sun) { return mon; } else { return sun; } } public String toString() { return this==sun?"sun":"mon"; }}?public class Static { public static void main(String[] args) { Weekday sun=Weekday.sun; System.out.println(sun.toString()); }}abstract class Weekday{ private Weekday(){} public final static Weekday sun=new Weekday(){ @Override public Weekday nextDay() { return mon; } }; public final static Weekday mon=new Weekday(){ @Override public Weekday nextDay() { return sun; }}; public abstract Weekday nextDay();/* public Weekday netxDay() { if (this==sun) { return mon; } else { return sun; } }*/ public String toString() { return this==sun?"sun":"mon"; }}?public static void main(String[] args) { WeekDay weekDay=WeekDay.mon; System.out.println(weekDay.valueOf("sun").toString()); } public enum WeekDay{ sun,mon; }枚举类的构造方法必须是私有类型。 public static void main(String[] args) { WeekDay weekDay=WeekDay.mon; System.out.println(weekDay.valueOf("sun").toString()); } public enum WeekDay{ sun(1),mon; private WeekDay(){System.out.println("first");} private WeekDay(int x){System.out.println("second");}; }输出结果:secondfirstsun枚举实现抽象方法:public enum WeekDay { sun(1){ @Override public WeekDay nextDay() { return mon; } }, mon(2){ @Override public WeekDay nextDay() { return sun; } }; public abstract WeekDay nextDay(); int time; private WeekDay(int time) { this.time = time; }; }?public class Test2 { public static void main(String[] args) { Person person=new Person(); Class class1 =person.getClass();//获得字节码方式1 try { Class class2 =Class.forName("Person");//获得字节码获得字节码方式2 } catch (ClassNotFoundException e) { e.printStackTrace(); } Class class3 =Person.class;//获得字节码方式三 }}class Person{}?public class ReflacterTest3 { public static void main(String[] args) throws Exception{ Zengxiao zengxiao=new Zengxiao(); changString(zengxiao); System.out.println(zengxiao); System.out.println(zengxiao.toString()); } private static void changString(Object obj)throws Exception { Field[] fields =obj.getClass().getFields(); for (Field field : fields) { if (field.getType()==String.class) { String oldValue=(String) field.get(obj); String newValue=oldValue.replace('b', 'a'); field.set(obj, newValue); } } };}class Zengxiao{ public String str1 ="ball"; public String str2 ="basketball"; public String str3 ="aaacc"; @Override public String toString() { return str1+":"+str2+":"+str3; }}Method Method method =String.class.getMethod("charAt", int.class);?public class ReflacterTest4 { static String str1="abc"; public static void main(String[] args){ Collection<Zeng> collection =new HashSet<Zeng>(); Zeng zeng1 =new Zeng(1, 2); Zeng zeng2 =new Zeng(1, 2); Zeng zeng3=new Zeng(3, 2); collection.add(zeng1); collection.add(zeng2); collection.add(zeng3); System.out.println(collection.size()); }}class Zeng{ public Zeng(int x, int y) { 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; Zeng other = (Zeng) obj; if (x != other.x) return false; if (y != other.y) return false; return true; }*/ int x; int y;}?public class ReflacterTest4 { static String str1="abc"; public static void main(String[] args) throws Exception{//一定要记住用完整的路径,但完整路径不是硬编码,而是运算出来的。 InputStream ipStream =new FileInputStream("config.properties"); Properties props =new Properties(); props.load(ipStream); ipStream.close();//关闭物理资源 String className =props.getProperty("className"); Collection<Zeng> collection =(Collection<Zeng>)Class.forName(className).newInstance(); //Collection<Zeng> collection =new HashSet<Zeng>(); Zeng zeng1 =new Zeng(1, 2); Zeng zeng2 =new Zeng(1, 2); Zeng zeng3=new Zeng(3, 2); collection.add(zeng1); collection.add(zeng2); collection.add(zeng3); System.out.println(collection.size()); }}class Zeng{ public Zeng(int x, int y) { this.x = x; this.y = y; } int x; int y;}?
public class IntroSpectorTest {/** * @param args */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubReflectPoint pt1 = new ReflectPoint(3,5);String propertyName = "x";//"x"-->"X"-->"getX"-->MethodGetX-->Object retVal = getProperty(pt1, propertyName);System.out.println(retVal);Object value = 7;setProperties(pt1, propertyName, value);System.out.println(BeanUtils.getProperty(pt1, "x").getClass().getName());BeanUtils.setProperty(pt1, "x", "9");System.out.println(pt1.getX());/*//java7????????Map map = {name:"zxx",age:18};BeanUtils.setProperty(map, "name", "lhm");*/BeanUtils.setProperty(pt1, "birthday.time", "111");System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));PropertyUtils.setProperty(pt1, "x", 9);System.out.println(PropertyUtils.getProperty(pt1, "x").getClass().getName());}private static void setProperties(Object 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(Object pt1, String propertyName)throws IntrospectionException, IllegalAccessException,InvocationTargetException {/*PropertyDescriptor pd = new PropertyDescriptor(propertyName,pt1.getClass());Method methodGetX = pd.getReadMethod();Object retVal = methodGetX.invoke(pt1);*/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;}}?@interface A{ }?
@Aclass b{}?
class c{ b.class.isAnnotaitonPresent(A.class); A a = B.class.getAnnotation(A.class);}?
public class AnnotationTest { @SuppressWarnings("deprecation")//压缩警告,告诉程序说,我知道已经过时了,不提示 public static void main(String[] args) { System.runFinalizersOnExit(true); sayHello(); if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){//判断是否是注解 //获取注解对象 ItcastAnnotation annotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class); System.out.println(annotation); System.out.println(annotation.color());//s使用注解的属性,调用方法 System.out.println(annotation.arrayArray().length);//s使用注解的属性,调用方法,数组属性不能转换为list打印出来 } } @Deprecated//表明该方法已过时 public static void sayHello(){ System.out.println("hi,传智播客"); } @Override//表明是覆盖方法,如果写错会提示 public String toString(){ return null; }} ?
?
?