package gen;
/**
- obiect 可以接受任意类型,因为发生多态
- @author Administrator
*/
public class Student {
private Object javase;
private Object oracle;
public Student() {
super();
}
public Student(Object javase, Object oracle) {
super();
this.javase = javase;
this.oracle = oracle;
}
public Object getJavase() {
return javase;
}
public void setJavase(Object javase) {
this.javase = javase;
}
public Object getOracle() {
return oracle;
}
public void setOracle(Object oracle) {
this.oracle = oracle;
}
}
//------------------------------------------------
package gen;
/**
- 问题:
- 1:强制类型转换
- 2:可能转换错误;运行异常,提示java.lang.ClassCastException;使用需要手动检查,避免错误
- @author Administrator
*/
public class App {
public static void main(String[] args) {
//存入整数 ;自动装箱int–>Integer–>object
Student stu = new Student(80,90);
int javaseScore = (Integer) stu.getJavase();//Score分数;强制类型转换
//String oracleScose = (String)stu.getOracle();//强制类型转换
//上面运行报出异常java.lang.ClassCastException
//手动检查避免错误
String oracleScose = null;
if(stu.getOracle() instanceof String){//instanceof运算符
oracleScose = (String)stu.getOracle();
}
System.out.println("分数为:"+javaseScore+","+oracleScose);
}
}
//-------------------------------------------------
package gen;
import gen.Student;
/**
- 问题:
- 1:强制类型转换
- 2:可能转换错误;运行异常,提示java.lang.ClassCastException;使用需要手动检查,避免错误
- @author Administrator
*/
public class App {
public static void main(String[] args) {
//存入整数 ;自动装箱int–>Integer–>object
Student stu = new Student(80,90);
int javaseScore = (Integer) stu.getJavase();//Score分数;强制类型转换
//String oracleScose = (String)stu.getOracle();//强制类型转换
//上面运行报出异常java.lang.ClassCastException
//手动检查避免错误
String oracleScose = null;
if(stu.getOracle() instanceof String){//instanceof运算符
oracleScose = (String)stu.getOracle();
}
System.out.println("分数为:"+javaseScore+","+oracleScose);
}
}
//-----------------------------------------------
package 泛型练习gen02;
/**
- 接口 :全局常量和公共的抽象方法
- 接口中 泛型字母只能使用在方法中,不能使用在全局常量中
- @author Administrator
- @param
/
public interface Comparator {
//泛型接口
void compare(T t);//compare比较
}
//---------------------------------------------------------------------
package 泛型练习gen02;
/* - 泛型方法<> 返回类型签名
-
只能访问对象的信息,不能修改信息
- 定义使用:<字母>
- 修饰符<字母>返回类型 方法名(字母){}
-
要定义泛型方法,只需要将于泛型参数列表置返回值前
- 注意:泛型还可以定义在方法中,是否拥有泛型方法,与其所在的类是否泛型没有关系
- @author Administrator
*/
public class TestMethod {//Method方法
public static void main(String[] args) {
test(“a”);//T–>自动转换字符串String
}
//泛型方法
public static void test(T a){
System.out.println(a);
}
}