国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

舟山建設(shè)信息港門戶網(wǎng)站seo網(wǎng)絡(luò)推廣技術(shù)員招聘

舟山建設(shè)信息港門戶網(wǎng)站,seo網(wǎng)絡(luò)推廣技術(shù)員招聘,網(wǎng)站建設(shè)如何插音樂,搜索網(wǎng)站logo怎么做一.單元測試 就是針對最小的功能單元(方法),編寫測試代碼對其進(jìn)行正確性測試 1.1 Junit單元測試框架 可以用來對方法進(jìn)行測試,它是第三方公司開源出來的(很多開發(fā)工具已經(jīng)集成了Junit框架,如IDEA&#xff…

一.單元測試

????????就是針對最小的功能單元(方法),編寫測試代碼對其進(jìn)行正確性測試

1.1 Junit單元測試框架

? ? ? ? 可以用來對方法進(jìn)行測試,它是第三方公司開源出來的(很多開發(fā)工具已經(jīng)集成了Junit框架,如IDEA)

優(yōu)點

????????可以靈活的編寫測試代碼,可以針對某個方法執(zhí)行測試,也支持一鍵完成對全部方法的自動化測試,且各自獨(dú)立

? ? ? ? 不需要程序員去分析測試的結(jié)果,會自動生成測試報告出來

//StringUtil
public class StringUtil {public static void printNumber(String name){if(name == null){System.out.println("名字長度:" + 0);return;}System.out.println("名字長度:" + name.length());}//求字符串的最大索引public static int getMaxIndex(String data){if(data == null){return -1;}return data.length()-1;}
}//StringUtilTest 
//測試類
public class StringUtilTest {@Test //測試方法的必須擁有@Test注解public void testPrintNumber(){StringUtil.printNumber("admin");StringUtil.printNumber(null);}@Test //測試方法的必須擁有@Test注解public void testGetMaxIndex(){int index1 = StringUtil.getMaxIndex(null);System.out.println(index1);int index2 = StringUtil.getMaxIndex("admin");System.out.println(index2);//斷言機(jī)制:程序員可以通過預(yù)測業(yè)務(wù)方法的結(jié)果Assert.assertEquals("方法內(nèi)有bug",4,index2);}
}

1.2 Junit框架的常用注解

在測試方法執(zhí)行前執(zhí)行的方法,常用于:初始化資源

在測試方法執(zhí)行完后再執(zhí)行的方法,常用于:釋放資源

//測試類
public class StringUtilTest {@Beforepublic void test1(){System.out.println("----> test1 Before 執(zhí)行了 -----------");}@Afterpublic void test2(){System.out.println("----> test2 After 執(zhí)行了 -----------");}@BeforeClasspublic static void test3(){System.out.println("----> test3 BeforeClass 執(zhí)行了 -----------");}@AfterClasspublic static void test4(){System.out.println("----> test4 AfterClass 執(zhí)行了 -----------");}@Test //測試方法的必須擁有@Test注解public void testPrintNumber(){StringUtil.printNumber("admin");StringUtil.printNumber(null);}@Test //測試方法的必須擁有@Test注解public void testGetMaxIndex(){int index1 = StringUtil.getMaxIndex(null);System.out.println(index1);int index2 = StringUtil.getMaxIndex("admin");System.out.println(index2);//斷言機(jī)制:程序員可以通過預(yù)測業(yè)務(wù)方法的結(jié)果Assert.assertEquals("方法內(nèi)有bug",4,index2);}
}

二.反射

????????反射(Reflection)就是加載類,并允許以編程的方式解剖類中的各種成分(成員變量、方法、構(gòu)造器等)

反射學(xué)什么?

????????學(xué)習(xí)獲取類的信息、操作它們

? ? ? ? ? ? 1.反射第一步:加載類,獲取類的字節(jié)碼:Class對象

? ? ? ? ? ? 2.獲取類的構(gòu)造器:Constructor對象

? ? ? ? ? ? 3.獲取類的成員變量:Field對象

? ? ? ? ? ? 4.獲取類的成員方法:Method對象

2.1 反射第一步:加載類,獲取類的字節(jié)碼:Class對象

獲取Class對象的三種方式

· Class c1 = 類名.class

·?調(diào)用class提供方法:public static Class forName(String package);

·?Object提供的方法:public Class getClass();? Class c3 = 對象.getClass();

//demo
public class demo {public static void main(String[] args) throws Exception {Class c1 = Student.class;System.out.println(c1.getName()); //全類名System.out.println(c1.getSimpleName()); //簡名Class c2 = Class.forName("com.wosun.jinjie.Student");System.out.println(c1 == c2);Student student = new Student();Class c3 = student.getClass();System.out.println(c3 == c1);}
}//Student
public class Student {
}

2.2 獲取類的構(gòu)造器:Constructor對象

獲取類構(gòu)造器的作用:依然是初始化對象返回

//TestConstructor
public class TestConstructor {//拿全部的構(gòu)造器@Testpublic void testGetConstructors() {//1.反射第一步:必須先得到這個類的Class對象Class c = Cat.class;//2.獲取類的全部構(gòu)造器//Constructor[] constructors = c.getConstructors(); //只能獲取public修飾的構(gòu)造器Constructor[] constructors = c.getDeclaredConstructors();  //只要存在的構(gòu)造器都能拿到//遍歷數(shù)組中的每個構(gòu)造器對象for (Constructor constructor : constructors) {System.out.println(constructor.getName() + "---->" + constructor.getParameterCount());}}//拿某個構(gòu)造器@Testpublic void testGetConstructor() throws Exception {//1.反射第一步:必須先得到這個類的Class對象Class c = Cat.class;//2.獲取類的某個構(gòu)造器:無參數(shù)構(gòu)造器//Constructor constructor1 = c.getConstructor();  //只能獲取public修飾的構(gòu)造器Constructor constructor1 = c.getDeclaredConstructor();  //只要存在的構(gòu)造器都能拿到System.out.println(constructor1.getName() + "---->" + constructor1.getParameterCount());constructor1.setAccessible(true); //禁止檢查訪問權(quán)限Cat cat1 = (Cat) constructor1.newInstance();System.out.println(cat1);//3.獲取類的某個構(gòu)造器:有參數(shù)構(gòu)造器//Constructor constructor2 = c.getConstructor(String.class , int.class);  //只能獲取public修飾的構(gòu)造器Constructor constructor2 = c.getDeclaredConstructor(String.class , int.class);System.out.println(constructor2.getName() + "---->" + constructor2.getParameterCount());constructor1.setAccessible(true); //禁止檢查訪問權(quán)限Cat cat2 = (Cat) constructor2.newInstance("叮當(dāng)貓", 3);System.out.println(cat2);}
}//Cat
public class Cat {private String name;private int age;public Cat() {System.out.println("無參數(shù)構(gòu)造器執(zhí)行了");}public Cat(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Cat{" +"name='" + name + '\'' +", age=" + age +'}';}
}

2.3 獲取類的成員變量:Field對象

獲取到成員變量的作用:依然是賦值、取值

//TestField
public class TestField {@Testpublic void TestGetFields() throws Exception {//1.反射第一步:必須先得到類的class對象Class c = Cat.class;//2.獲取類的全部成員變量Field[] fields = c.getDeclaredFields();//3.遍歷成員變量數(shù)組for (Field field : fields) {System.out.println(field.getName() + "---->" + field.getType());}//4.定位某個成員變量Field fName = c.getDeclaredField("name");System.out.println(fName.getName() + "===>" + fName.getType());Field fAge = c.getDeclaredField("age");System.out.println(fAge.getName() + "===>" + fAge.getType());//賦值Cat cat = new Cat();fName.setAccessible(true);fName.set(cat, "咖啡貓");System.out.println(cat);//取值String name = (String) fName.get(cat);System.out.println(name);}
}//Cat
public class Cat {private static int a;public static final String COUNTRY = "CHN";private String name;private int age;public Cat() {System.out.println("無參數(shù)構(gòu)造器執(zhí)行了");}public Cat(String name, int age) {System.out.println("有參數(shù)構(gòu)造器執(zhí)行了");this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Cat{" +"name='" + name + '\'' +", age=" + age +'}';}
}

2.4 獲取類的成員方法:Method對象

成員方法的作用:依然是執(zhí)行

//TestMethod
public class TestMethod {@Testpublic void TestGetMethods() throws Exception {//1.反射第一步:先得到Class對象Class c = Cat.class;//2.獲取類的全部成員方法Method[] methods = c.getDeclaredMethods();//3.遍歷數(shù)組中的每個方法對象for (Method method : methods) {System.out.println(method.getName() + "===>" + method.getParameterCount() + "===>" + method.getReturnType());}Method method_run = c.getDeclaredMethod("run");System.out.println(method_run.getName() + "===>" + method_run.getParameterCount() + "===>" + method_run.getReturnType());Method method_eat =c.getDeclaredMethod("eat", String.class);System.out.println(method_eat.getName() + "===>" + method_eat.getParameterCount() + "===>" + method_eat.getReturnType());Cat cat = new Cat();method_run.setAccessible(true);Object rs1 = method_run.invoke(cat);//調(diào)用無參的run方法,用cat對象觸發(fā)調(diào)用的System.out.println(rs1);Object rs2 = method_eat.invoke(cat,"🐟");System.out.println(rs2);}
}//Cat
public class Cat {private static int a;public static final String COUNTRY = "CHN";private String name;private int age;public Cat() {System.out.println("無參數(shù)構(gòu)造器執(zhí)行了");}public Cat(String name, int age) {System.out.println("有參數(shù)構(gòu)造器執(zhí)行了");this.name = name;this.age = age;}private void run(){System.out.println("跑的真快");}public void eat(){System.out.println("愛吃貓糧");}public String eat(String name){return "貓最愛吃" + name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Cat{" +"name='" + name + '\'' +", age=" + age +'}';}
}

2.5 反射的作用、應(yīng)用場景

反射的作用

· 基本作用:可以得到一個類的全部成分然后操作

· 可以破壞封裝性

· 最重要的用途是:適合做Java的框架,基本上主流的框架都會基于反射設(shè)計出一些通用的功能

//TestFrame
public class TestFrame {@Testpublic void save() throws Exception {Student s1 = new Student("張三",22,'男',180.5,"編程");Teacher t1 = new Teacher("李四",1000000);//需求:把任意對象的字段名和其對應(yīng)的值等信息,保存到文件中ObjectFrame.saveObject(s1);ObjectFrame.saveObject(t1);}
}//ObjectFrame
public class ObjectFrame {//目的:保存任意對象的字段和其數(shù)據(jù)到文件中去public static void saveObject(Object obj) throws Exception {//創(chuàng)建打印流PrintStream ps = new PrintStream(new FileOutputStream("src\\data.txt",true));//obj是任意對象,到底有多少個字段要保存呢?Class c = obj.getClass();String cName = c.getSimpleName();ps.println("---------" + cName + "---------");//從這個類中提取它的全部成員變量Field[] fields = c.getDeclaredFields();//遍歷每個成員變量for (Field field : fields) {//拿到成員變量的名字String name = field.getName();//拿到這個成員變量在對象中的數(shù)據(jù)field.setAccessible(true); //禁止檢查訪問控制String value = field.get(obj) + "";ps.println(name + "=" + value);}ps.close();}
}//Student
public class Student {private String name;private int age;private char sex;private double height;private String hobby;public Student() {}public Student(String name, int age, char sex, double height, String hobby) {this.name = name;this.age = age;this.sex = sex;this.height = height;this.hobby = hobby;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;}
}//Teacher
public class Teacher {private String name;private double salary;public Teacher() {}public Teacher(String name, double salary) {this.name = name;this.salary = salary;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}
}

三.注解

????????注解(Annotation)就是Java代碼里的特殊標(biāo)記,比如:@Override、@Test等,作用是讓其他程序根據(jù)注解信息來決定怎么執(zhí)行該程序

· 注意:注解可以用在類上、構(gòu)造器上、方法上、成員變量上、參數(shù)上、等位置處

自定義注解

public @interface 注解名稱 {

? ? ? ? public 屬性類型 屬性名() default 默認(rèn)值;

}

特殊屬性名:value

· 如果注解中只有一個value屬性,使用注解后,value名稱可以不寫!!

//AnnotationTest @MyTest1(aaa = "牛魔王", ccc = {"java","python"})
// @MyTest2(value = "孫悟空")
// @MyTest2("孫悟空") //如果只有一個屬性,可以不寫value
// @MyTest2(value = "孫悟空", age = 100)
@MyTest2("孫悟空") //如果除了value其他屬性都有默認(rèn)值,也可以不寫value
public class AnnotationTest {@MyTest1(aaa = "鐵扇公主", bbb = false, ccc = {"c","c++"})public void test1(){}
}/*** 自定義注解*/
public @interface MyTest1 {String aaa();boolean bbb() default true;String[] ccc();
}public @interface MyTest2 {String value(); //特殊屬性int age() default 22;
}

· 注解本質(zhì)是一個接口,Java中所有注解都繼承了Annotation接口的

· @注解(…):其實就是一個實現(xiàn)類對象,實現(xiàn)了該注解以及Annotation接口

3.1 元注解

指的是:修飾注解的注解

//MyTest3@Target({ElementType.TYPE, ElementType.METHOD}) //ElementType.TYPE當(dāng)前被修飾的注解只能用在類上
@Retention(RetentionPolicy.RUNTIME) //控制下面的注解一直保留到運(yùn)行時
public @interface MyTest3 {
}

3.2 注解的解析

????????就是判斷類上、方法上、成員變量上是否存在注解,并把注解里的內(nèi)容給解析出來

如何解析注解?

· 指導(dǎo)思想:要解析誰上面的注解,就應(yīng)該先拿到誰

· 比如要解析類上面的注解,則應(yīng)該先獲取該類的Class對象,再通過Class對象解析其上面的注解

· 比如要解析成員方法上的注解,則應(yīng)該獲取到該成員方法的method對象,再通過Method對象解析其上面的注解

· Class、Method、Field、Constructor都實現(xiàn)了AnnotatedElement接口,它們都擁有解析注釋的能力

//MyTest4@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTest4 {String value();double aaa() default 100;String[] bbb();
}//demo@MyTest4(value = "張三", aaa = 99, bbb={"java","python"})
public class demo {@MyTest4(value = "李四", bbb={"c","c++"})public void test1(){}
}//AnnotationTest2 public class AnnotationTest2 {@Testpublic void parseClass(){//1.先得到Class對象Class c  = demo.class;//2.解析類上的注解//判斷類上是否包含了某個注解if(c.isAnnotationPresent(MyTest4.class)){MyTest4 myTest4 = (MyTest4) c.getDeclaredAnnotation(MyTest4.class);System.out.println(myTest4.value());System.out.println(myTest4.aaa());System.out.println(Arrays.toString(myTest4.bbb()));}}@Testpublic void parseMethod() throws Exception {//1.先得到Class對象Class c  = demo.class;Method m = c.getDeclaredMethod("test1");//2.解析類上的注解//判斷方法上是否包含了某個注解if(m.isAnnotationPresent(MyTest4.class)){MyTest4 myTest4 = (MyTest4) m.getDeclaredAnnotation(MyTest4.class);System.out.println(myTest4.value());System.out.println(myTest4.aaa());System.out.println(Arrays.toString(myTest4.bbb()));}}
}

3.3 注解的應(yīng)用場景

//MyTest@Target(ElementType.METHOD) //只能注解方法
@Retention(RetentionPolicy.RUNTIME) //讓當(dāng)前注解可以一直存活
public @interface MyTest {}//AnnotationTest
public class AnnotationTest {// @MyTestpublic void test1(){System.out.println("=====test1=====");}@MyTestpublic void test2(){System.out.println("=====test2=====");}// @MyTestpublic void test3(){System.out.println("=====test3=====");}@MyTestpublic void test4(){System.out.println("=====test4=====");}public static void main(String[] args) throws Exception {AnnotationTest t = new AnnotationTest();//啟動程序//1.得到class對象Class c = AnnotationTest.class;//2.提取類中的全部成員方法Method[] methods = c.getDeclaredMethods();//3.遍歷這個數(shù)組中的每個方法,看這個方法是否存在@Mytest注解for (Method method : methods) {if(method.isAnnotationPresent(MyTest.class)){//說明當(dāng)前方法上是存在@MyTestmethod.invoke(t);}}}
}

四.動態(tài)代理

程序為什么需要代理?

????????對象如果嫌身上干的事情太多,可以通過代理來轉(zhuǎn)移部分職責(zé)

代理長什么樣?

????????對象有什么方法想被代理,代理就一定要有對應(yīng)的方法

中介如何知道要派有什么樣方法的代理呢?

????????接口!

//Test
public class Test {public static void main(String[] args) {BigStar bigStar = new BigStar("張三");Star starProxy = ProxyUtil.createProxy(bigStar);String rs = starProxy.sing("好日子");System.out.println(rs);starProxy.dance();}
}//ProxyUtil 
public class ProxyUtil {public static Star createProxy(BigStar bigStar){/***         public static Object newProxyInstance(ClassLoader loader,*                 Class<?>[] interfaces,*                 InvocationHandler h)*                 參數(shù)1:用于指定一個類加載器*                 參數(shù)2:指定生成的代理長什么樣子,也就是有哪些方法*                 參數(shù)3:用來指定生成的代理對象想干什么事情*/Star startProxy = (Star)Proxy.newProxyInstance(ProxyUtil.class.getClassLoader(),new Class[]{Star.class}, new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//代理對象要做的事情if(method.getName().equals("sing")){System.out.println("收錢20w");//return method.invoke(bigStar,args);}else if(method.getName().equals("dance")){System.out.println("收錢30w");//return method.invoke(bigStar,args);}return method.invoke(bigStar,args);}});return startProxy;}
}//Star
public interface Star {String sing(String name);void dance();
}//BIgStar
public class BigStar implements Star{private String name;public BigStar(String name) {this.name = name;}public String sing(String name){System.out.println(this.name + "正在唱" +name);return "thanks";}public void dance(){System.out.println(this.name + "正在跳舞");}
}
http://m.aloenet.com.cn/news/44191.html

相關(guān)文章:

  • 成都疫情實時狀況seo搜索優(yōu)化 指數(shù)
  • 外管局網(wǎng)站上做預(yù)收登記廊坊seo
  • 網(wǎng)站安全如何做百度 營銷推廣多少錢
  • tomcat做網(wǎng)站站長之家查詢的網(wǎng)址
  • 十堰微網(wǎng)站建設(shè)淘寶自動推廣軟件
  • 淘寶客做的比較好的網(wǎng)站友情鏈接有哪些作用
  • 網(wǎng)站中的圖片必須用 做嗎網(wǎng)站建設(shè)純免費(fèi)官網(wǎng)
  • 個人網(wǎng)站怎么建設(shè)關(guān)鍵詞分為哪幾類
  • 網(wǎng)站策劃怎么做內(nèi)容環(huán)球軍事網(wǎng)
  • 網(wǎng)站制作怎樣做背景常用的網(wǎng)絡(luò)推廣方法有
  • 旅游網(wǎng)站建設(shè)系統(tǒng)百度一下官網(wǎng)頁
  • 企業(yè)做網(wǎng)站設(shè)計百度seo營銷推廣
  • 如何自己做網(wǎng)站一年賺一億東莞推廣公司
  • cms 開源持續(xù)優(yōu)化疫情防控舉措
  • 買了兩臺服務(wù)器可以做網(wǎng)站嗎濟(jì)南優(yōu)化網(wǎng)頁
  • 用織夢做的網(wǎng)站怎么上傳虛擬網(wǎng)絡(luò)營銷的實現(xiàn)方式有哪些
  • 營銷助手appseo網(wǎng)絡(luò)營銷的技術(shù)
  • 建一個網(wǎng)站邁年廣州白云區(qū)最新信息
  • 啥是深圳網(wǎng)站建設(shè)com域名多少錢一年
  • 金融公司網(wǎng)站開發(fā)汕頭seo計費(fèi)管理
  • 生活做爰網(wǎng)站北京seo優(yōu)化wyhseo
  • 國家安全人民防線建設(shè)網(wǎng)站如何在網(wǎng)上推廣產(chǎn)品
  • wordpress如何發(fā)布視頻seo搜索引擎工具
  • wordpress+站群軟件東莞網(wǎng)絡(luò)推廣營銷公司
  • 關(guān)注網(wǎng)站制作seo診斷
  • 中小企業(yè)建站模板百度免費(fèi)發(fā)布信息
  • 石巖小學(xué)網(wǎng)站建設(shè)網(wǎng)絡(luò)營銷策劃書的主要內(nèi)容
  • 石家莊網(wǎng)站開發(fā)設(shè)計百度網(wǎng)站下載
  • 做甜品網(wǎng)站的需求分析網(wǎng)站如何發(fā)布
  • 如何給網(wǎng)站做流量百度推廣網(wǎng)址是多少