面向对象特征之二:继承性 为描述和处理个人信息,定义类Person
:
1 2 3 4 5 6 7 8 class Person { public String name; public int age; public Date birthDate; public String getInfo () { } }
为描述和处理学生信息,定义类Student:
1 2 3 4 5 6 7 8 9 class Student { public String name; public int age; public Date birthDate; public String school; public String getInfo () { } }
通过继承,简化Student类的定义:
1 2 3 4 5 6 7 8 9 10 11 class Person { public String name; public int age; public Date birthDate; public String getInfo () { } }class Student extends Person { public String school; }
Student类继承了父类Person的所有属性和方法,并增加了一个属性school。Person中的属性和方法,Student都可以使用。
为什么要有继承? 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可。
此处的多个类称为子类(派生类),单独的这个类称为父类(基类或超类)。可以理解为:“子类 is a 父类”
类继承语法规则:class Subclass extends SuperClass{ }
子类继承了父类,就继承了父类的方法和属性。
在子类中,可以使用父类中定义的方法和属性,也可以创建新的数据和方法。
在Java 中,继承的关键字用的是“extends”,即子类不是父类的子集,而是对父类的“扩展”。
关于继承的规则 :
子类不能直接访问父类中私有的(private)的成员变量和方法。
Java只支持单继承和多层继承,不允许多重继承
一个子类只能有一个父类
一个父类可以派生出多个子类
方法的重写(override) 定义 :在子类中可以根据需要对从父类中继承来的方法进行改造,也称为方法的重置、覆盖 。在程序执行时,子类的方法将覆盖父类的方法。
要求 :
子类重写的方法必须和父类被重写的方法具有相同的方法名称、参数列表
子类重写的方法的返回值类型不能大于父类被重写的方法的返回值类型
子类重写的方法使用的访问权限不能小于父类被重写的方法的访问权限
子类方法抛出的异常不能大于父类被重写方法的异常
注意 : 子类与父类中同名同参数的方法必须同时声明为非static的(即为重写),或者同时声明为static的(不是重写)。因为static方法是属于类的,子类无法覆盖父类的方法 。
重写方法举例(1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class Person { public String name; public int age; public String getInfo () { return "Name: " + name + "\n" +"age: " + age; } }public class Student extends Person { public String school; public String getInfo () { return "Name: " + name + "\nage: " + age + "\nschool: " + school; } public static void main (String args[]) { Student s1=new Student(); s1.name="Bob" ; s1.age=20 ; s1.school="school2" ; System.out.println(s1.getInfo()); } }
重写方法举例(2) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Parent { public void method1 () {} }class Child extends Parent { private void method1 () {} }public class UseBoth { public static void main (String[] args) { Parent p1 = new Parent(); Child c1 = new Child(); p1.method1(); c1.method1(); } }
四种访问权限修饰符 Java权限修饰符public、protected、 (缺省)、 private置于类的成员定义前,用来限定对象对该类成员的访问权限。
对于class的权限修饰只可以用public
和default
(缺省)。
public
类可以在任意地方被访问。
default
类只可以被同一个包内部的类访问。
访问控制举例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 class Parent { private int f1 = 1 ; int f2 = 2 ; protected int f3 = 3 ; public int f4 = 4 ; private void fm1 () {System.out.println("in fm1() f1=" + f1);} void fm2 () {System.out.println("in fm2() f2=" + f2);} protected void fm3 () {System.out.println("in fm3() f3=" + f3);} public void fm4 () {System.out.println("in fm4() f4=" + f4);} }class Child extends Parent { private int c1 = 21 ; public int c2 = 22 ; private void cm1 () {System.out.println("in cm1() c1=" + c1);} public void cm2 () {System.out.println("in cm2() c2=" + c2);} public static void main (String[] args) { int i; Parent p = new Parent(); i = p.f2; i = p.f3; i = p.f4; p.fm2(); p.fm3(); p.fm4(); Child c = new Child(); i = c.f2; i = c.f3; i = c.f4; i = c.c1; i = c.c2; c.cm1(); } }
关键字:super 在Java类中使用super
来调用父类中的指定操作:
super可用于访问父类中定义的属性
super可用于调用父类中定义的成员方法
super可用于在子类构造器中调用父类的构造器
注意 :
尤其当子父类出现同名成员时,可以用super表明调用的是父类中的成员
super的追溯不仅限于直接父类
super和this的用法相像,this代表本类对象的引用,super代表父类的内存空间的标识
关键字super举例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Person { protected String name = "张三" ; protected int age; public String getInfo () { return "Name: " + name + "\nage: " + age; } }class Student extends Person { protected String name = "李四" ; private String school = "New Oriental" ; public String getSchool () { return school; } public String getInfo () { return super .getInfo() + "\nschool: " + school; }}public class StudentTest { public static void main (String[] args) { Student st = new Student(); System.out.println(st.getInfo()); }}
调用父类的构造器
子类中所有的构造器默认都会访问父类中空参数的构造器
当父类中没有空参数的构造器时,子类的构造器必须通过 this(参数列表)或者super(参数列表) 语句指定调用本类或者父类中相应的构造器。同时,只能”二选一”,且必须放在构造器的首行
如果子类构造器中既未显式调用父类或本类的构造器,且父类中又没有无参的构造器,则编译出错 。
调用父类构造器举例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class Person { private String name; private int age; private Date birthDate; public Person (String name, int age, Date d) { this .name = name; this .age = age; this .birthDate = d; } public Person (String name, int age) { this (name, age, null ); } public Person (String name, Date d) { this (name, 30 , d); } public Person (String name) { this (name, 30 ); } public class Student extends Person { private String school; public Student (String name, int age, String s) { super (name, age); school = s; } public Student (String name, String s) { super (name); school = s; } public Student (String s) { school = s; } }
this和super的区别
子类对象实例化过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Creature { public Creature () { System.out.println("Creature无参数的构造器" ); }}class Animal extends Creature { public Animal (String name) { System.out.println("Animal带一个参数的构造器,该动物的name为" + name); } public Animal (String name, int age) { this (name); System.out.println("Animal带两个参数的构造器,其age为" + age); }}public class Wolf extends Animal { public Wolf () { super ("灰太狼" , 3 ); System.out.println("Wolf无参数的构造器" ); } public static void main (String[] args) { new Wolf(); }}
OOP特征三:多态性 多态性,是面向对象中最重要的概念,在Java中的体现:
对象的多态性:父类的引用指向子类的对象
可以直接应用在抽象类和接口上
Java引用变量有两个类型:编译时类型 和运行时类型 。编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定。简称:编译时,看左边;运行时,看右边 。
若编译时类型和运行时类型不一致,就出现了对象的多态性(Polymorphism)
多态情况下,“看左边”:看的是父类的引用(父类中不具备子类特有的方法)“看右边”:看的是子类的对象(实际运行的是子类重写父类的方法)
对象的多态 —在Java中,子类的对象可以替代父类的对象使用
一个变量只能有一种确定的数据类型
一个引用类型变量可能指向(引用)多种不同类型的对象1 2 3 Person p = new Student(); Object o = new Person(); o = new Student();
子类可看做是特殊的父类,所以父类类型的引用可以指向子类的对象:向上转型(upcasting)。
一个引用类型变量如果声明为父类的类型,但实际引用的是子类对象,那么该变量就不能再访问子类中添加的属性和方法
1 2 3 4 5 6 Student m = new Student(); m.school = “pku”; Person e = new Student(); e.school = “pku”;
属性是在编译时确定的,编译时e为Person类型,没有school成员变量,因而编译错误。
多态性应用举例 方法声明的形参类型为父类类型,可以使用子类的对象作为实参调用该方法
1 2 3 4 5 6 7 8 9 10 11 public class Test { public void method (Person e) { e.getInfo(); } public static void main (Stirng args[]) { Test t = new Test(); Student m = new Student(); t.method(m); } }
虚拟方法调用(Virtual Method Invocation) 正常的方法调用
1 2 3 4 Person e = new Person(); e.getInfo(); Student e = new Student(); e.getInfo();
虚拟方法调用(多态情况下) 子类中定义了与父类同名同参数的方法,在多态情况下,将此时父类的方法称为虚拟方法,父类根据赋给它的不同子类对象,动态调用属于子类的该方法。这样的方法调用在编译期是无法确定的。
1 2 Person e = new Student(); e.getInfo();
编译时类型和运行时类型 编译时e为Person类型,而方法的调用是在运行时确定的,所以调用的是Student类的getInfo()
方法。——动态绑定
虚拟方法调用举例 前提: Person类中定义了welcome()
方法,各个子类重写了welcome()
。
执行: 多态的情况下,调用对象的welcome()
方法,实际执行的是子类重写的方法。
小结:方法的重载与重写 从编译和运行的角度看: 重载,是指允许存在多个同名方法,而这些方法的参数不同。编译器根据方法不同的参数表,对同名方法的名称做修饰。对于编译器而言,这些同名方法就成了不同的方法。它们的调用地址在编译期就绑定了。Java的重载是可以包括父类和子类的,即子类可以重载父类的同名不同参数的方法。所以:对于重载而言,在方法调用之前,编译器就已经确定了所要调用的方法,这称为“早绑定”或“静态绑定”;而对于多态,只有等到方法调用的那一刻,解释运行器才会确定所要调用的具体方法,这称为“晚绑定”或“动态绑定”。
引用一句Bruce Eckel的话:“不要犯傻,如果它不是晚绑定,它就不是多态 。”
多态小结 多态作用 :
前提:
成员方法:
编译时:要查看引用变量所声明的类中是否有所调用的方法。
运行时:调用实际new的对象所属的类中的重写方法。
成员变量:
instanceof 操作符 x instanceof A:检验x是否为类A的对象,返回值为boolean
型。
要求x所属的类与类A必须是子类和父类的关系,否则编译错误。
如果x属于类A的子类B,x instanceof A值也为true。
1 2 3 4 5 6 7 8 9 10 11 12 public class Person extends Object {…}public class Student extends Person {…}public class Graduate extends Person {…} -------------------------------------------------------------------public void method1 (Person e) { if (e instanceof Person) if (e instanceof Student) if (e instanceof Graduate) }
对象类型转换(Casting) 基本数据类型的Casting:
自动类型转换 :小的数据类型可以自动转换成大的数据类型 如long g=20;
double d=12.0f
强制类型转换 :可以把大的数据类型强制转换(casting)成小的数据类型 如 float f=(float)12.0; int a=(int)1200L
对Java对象的强制类型转换称为造型
从子类到父类的类型转换可以自动进行
从父类到子类的类型转换必须通过造型(强制类型转换)实现
无继承关系的引用类型间的转换是非法的
在造型前可以使用instanceof操作符测试一个对象的类型
对象类型转换举例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.javami.day14;public class ConversionTest { public static void main (String[] args) { double d = 13.4 ; long l = (long ) d; System.out.println(l); int in = 5 ; Object obj = "Hello" ; String objStr = (String) obj; System.out.println(objStr); Object objPri = new Integer(5 ); String str = (String) objPri; } }
对象类型转换举例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.javami.day14;public class Test { public void method (Person e) { if (e instanceof Student) { Student me = (Student) e; System.out.pritnln(me.getschool()); } } public static void main (String[] args) { Test t = new Test(); Student m = new Student(); t.method(m); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.javami.day14;class Base { int count = 10 ; public void display () { System.out.println(this .count); } }class Sub extends Base { int count = 20 ; public void display () { System.out.println(this .count); } }public class FieldMethodTest { public static void main (String[] args) { Sub s = new Sub(); System.out.println(s.count); s.display(); Base b = s; System.out.println(b == s); System.out.println(b.count); b.display(); } }
子类继承父类
若子类重写了父类方法,就意味着子类里定义的方法彻底覆盖了父类里的同名方法,系统将不可能把父类里的方法转移到子类中。
对于实例变量则不存在这样的现象,即使子类里定义了与父类完全相同的实例变量,这个实例变量依然不可能覆盖父类中定义的实例变量。
多态性练习 定义三个类,父类GeometricObject
代表几何形状,子类Circle
代表圆形,MyRectangle
代表矩形。定义一个测试类GeometricTest
,编写equalsArea
方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术),编写displayGeometricObject
方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 package com.javami.day14;class GeometricObject { public void setColor (String color) { this .color = color; } public void setWeight (double weight) { this .weight = weight; } private String color; private double weight; public String getColor () { return color; } public GeometricObject (String color, double weight) { super (); this .color = color; this .weight = weight; } public double getWeight () { return weight; } public double findArea () { return 0.0 ; } }class Circle extends GeometricObject { private double radius; public Circle (String color, double weight, double radius) { super (color, weight); this .radius = radius; } public double getRadius () { return radius; } public void setRadius (double radius) { this .radius = radius; } public double findArea () { return 3.14 * radius * radius; } }class MyRectangle extends GeometricObject { private double width; private double height; public MyRectangle (String color, double weight, double width, double height) { super (color, weight); this .width = width; this .height = height; } public double getWidth () { return width; } public void setWidth (double width) { this .width = width; } public double getHeight () { return height; } public void setHeight (double height) { this .height = height; } @Override public double findArea () { return width * height; } }public class GeometricTest { public static void main (String[] args) { GeometricTest test = new GeometricTest(); Circle c1 = new Circle( "white" , 3.3 ,1.0 ); test.displayGeometricObject(c1); Circle c2 = new Circle( "white" , 3.3 ,1.0 ); test.displayGeometricObject(c2); boolean isEquals = test.equalsArea(c1, c2); System.out.println("c1 和 c2的面积是否相等:" + isEquals); MyRectangle rect = new MyRectangle("red" ,2.1 , 3.4 ,2.0 ); test.displayGeometricObject(rect); } public boolean equalsArea (GeometricObject o1,GeometricObject o2) { return o1.findArea() == o2.findArea(); } public void displayGeometricObject (GeometricObject o) { System.out.println("面积为:" + o.findArea()); } }
面试题:多态是编译时行为还是运行时行为?如何证明?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 package com.javami.day14;import java.util.Random;class Animal { protected void eat () { System.out.println("animal eat food" ); } }class Cat extends Animal { protected void eat () { System.out.println("cat eat fish" ); } }class Dog extends Animal { public void eat () { System.out.println("Dog eat bone" ); } }class Sheep extends Animal { public void eat () { System.out.println("Sheep eat grass" ); } }public class InterviewTest { public static Animal getInstance (int key) { switch (key) { case 0 : return new Cat (); case 1 : return new Dog (); default : return new Sheep (); } } public static void main (String[] args) { int key = new Random().nextInt(3 ); System.out.println(key); Animal animal = getInstance(key); animal.eat(); } }
Object类的使用
Object
类是所有Java
类的根父类
如果在类的声明中未使用extends关键字指明其父类,则默认父类为java.lang.Object
类
1 public class Person {...}
等价于:
1 public class Person extends Object {...}
例:
1 2 3 method(Object obj){…} Person o=new Person(); method(o);
Object类中的主要结构
==操作符与equals方法 ==
:
基本类型比较值:只要两个变量的值相等,即为true
。int a=5; if(a==6){…}
引用类型比较引用(是否指向同一个对象):只有指向同一个对象时,==才返回true。1 2 3 Person p1=new Person(); Person p2=new Person();if (p1==p2){…}
用“==”进行比较时,符号两边的数据类型必须兼容(可自动转换的基本数据类型除外),否则编译出错
equals()
:所有类都继承了Object,也就获得了equals()
方法。还可以重写。
只能比较引用类型,其作用与“==”相同,比较是否指向同一个对象。
格式:obj1.equals(obj2)
特例:当用equals()
方法进行比较时,对类File、String、Date及包装类(Wrapper Class)来说,是比较类型及内容而不考虑引用的是否是同一个对象;
原因:在这些类中重写了Object类的equals()
方法。 当自定义使用equals()
时,可以重写。用于比较两个对象的“内容”是否都相等
重写equals()方法的原则
对称性 :如果x.equals(y)
返回是“true”,那么y.equals(x)
也应该返回是“true”。
自反性 :x.equals(x)
必须返回是“true”。
传递性 :如果x.equals(y)
返回是“true”,而且y.equals(z)返回是“true”,那么z.equals(x)
也应该返回是“true”。
一致性 :如果x.equals(y)
返回是“true”,只要x和y内容一直不变,不管你重复x.equals(y)
多少次,返回都是“true”。
任何情况下,x.equals(null)
,永远返回是“false
”;x.equals
(和x不同类型的对象)永远返回是“false”。
1 2 3 4 5 6 7 8 9 10 11 int it = 65 ;float fl = 65.0f ; System.out.println(“65 和65.0f 是否相等?” + (it == fl)); char ch1 = 'A' ; char ch2 = 12 ; System.out.println("65和'A'是否相等?" + (it == ch1)); System.out.println(“12 和ch2是否相等?" + (12 == ch2));//true String str1 = new String(" hello"); String str2 = new String(" hello"); System.out.println(" str1和str2是否相等?"+ (str1 == str2));//false System.out.println(" str1是否equals str2?"+(str1.equals(str2)));//true System.out.println(“hello” == new java.util.Date()); //编译不通过
请根据以下代码自行定义能满足需要的MyDate类,在MyDate类中覆盖equals方法,使其判断当两个MyDate类型对象的年月日都相同时,结果为true,否则为false。public boolean equals(Object o)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 package com.javami.day14;import java.util.Objects;class MyDate { private int year; private int month; private int day; public int getYear () { return year; } public void setYear (int year) { this .year = year; } public int getMonth () { return month; } public void setMonth (int month) { this .month = month; } public int getDay () { return day; } public void setDay (int day) { this .day = day; } @Override public boolean equals (Object o) { if (this == o) return true ; if (!(o instanceof MyDate)) return false ; MyDate myDate = (MyDate) o; return year == myDate.year && month == myDate.month && day == myDate.day; } @Override public int hashCode () { return Objects.hash(year, month, day); } public MyDate (int year, int month, int day) { this .year = year; this .month = month; this .day = day; } }public class EqualsTest { public static void main (String[] args) { MyDate m1 = new MyDate(14 , 3 , 1976 ); MyDate m2 = new MyDate(14 , 3 , 1976 ); if (m1 == m2) { System.out.println("m1==m2" ); } else { System.out.println("m1!=m2" ); } if (m1.equals(m2)) { System.out.println("m1 is equal to m2" ); } else { System.out.println("m1 is not equal to m2" ); } } }
toString() 方法
toString()
方法在Object类中定义,其返回值是String类型,返回类名和它的引用地址。
在进行String与其它类型数据的连接操作时,自动调用toString()
方法1 2 3 Date now=new Date(); System.out.println(“now=”+now); 相当于 System.out.println(“now=”+now.toString());
可以根据需要在用户自定义类型中重写toString()
方法 如String 类重写了toString()
方法,返回字符串的值。1 2 s1=“hello”; System.out.println(s1);
基本类型数据转换为String
类型时,调用了对应包装类的toString()
方法1 2 int a=10 ; System.out.println(“a=”+a);
包装类(Wrapper)的使用 针对八种基本数据类型定义相应的引用类型—包装类(封装类),有了类的特点,就可以调用类中的方法,Java才是真正的面向对象。
基本数据类型包装成包装类的实例—装箱
通过包装类的构造器实现:int i = 500; Integer t = new Integer(i);
还可以通过字符串参数构造包装类对象:1 2 Float f = new Float(“4.56 ”); Long l = new Long(“asdf”);
获得包装类对象中包装的基本类型变量—拆箱
调用包装类的.xxxValue()
方法:boolean b = bObj.booleanValue();
JDK1.5之后,支持自动装箱,自动拆箱。但类型必须匹配。
字符串转换成基本数据类型
通过包装类的构造器实现:int i = new Integer(“12”);
通过包装类的parseXxx(String s)
静态方法:Float f = Float.parseFloat(“12.1”);
基本数据类型转换成字符串
调用字符串重载的valueOf()方法:String fstr = String.valueOf(2.34f);
更直接的方式:String intStr = 5 + “”
包装类用法举例 1 2 int i = 500 ; Integer t = new Integer(i);
装箱:包装类使得一个基本数据类型的数据变成了类。 有了类的特点,可以调用类中的方法。
拆箱:将数字包装类中内容变为基本数据类型。
包装类在实际开发中用的最多的在于字符串变为基本数据类型。
1 2 3 4 5 String str1 = "30" ; String str2 = "30.3" ;int x = Integer.parseInt(str1) ;float f = Float.parseFloat(str2) ;
1 2 3 4 String s = t.toString(); String s1 = Integer.toString(314 ); String s2=“4.56 ”;double ds=Double.parseDouble(s2);
包装类(Wrapper)的面试题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Test public void test3 () { Integer i = new Integer(1 ); Integer j = new Integer(1 ); System.out.println(i == j); Integer m = 1 ; Integer n = 1 ; System.out.println(m == n); Integer x = 128 ; Integer y = 128 ; System.out.println(x == y); }
包装类(Wrapper)的练习 利用Vector
代替数组处理:从键盘读入学生成绩(以负数代表输入结束),找出最高分,并输出学生成绩等级。
提示:数组一旦创建,长度就固定不变,所以在创建数组前就需要知道它的长度。而向量类java.util.Vector
可以根据需要动态伸缩。
创建Vector对象:Vector v=new Vector();
给向量添加元素:v.addElement(Object obj); //obj必须是对象
取出向量中的元素:Object obj=v.elementAt(0);
注意第一个元素的下标是0,返回值是Object类型的。
计算向量的长度:v.size();
若与最高分相差10分内:A等;20分内:B等;30分内:C等;其它:D等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package com.javami.day14;import java.util.Scanner;import java.util.Vector;public class ScoreTest { public static void main (String[] args) { Scanner scan = new Scanner(System.in); Vector v = new Vector(); int maxScore = 0 ; for (;;){ System.out.println("请输入学生成绩(以负数代表输入结束)" ); int score = scan.nextInt(); if (score < 0 ){ break ; } if (score > 100 ){ System.out.println("输入的数据非法,请重新输入" ); continue ; } v.add(score); if (maxScore < score){ maxScore = score; } } char level; for (int i = 0 ;i < v.size();i++){ Object obj = v.elementAt(i); int score = (int )obj; if (maxScore - score <= 10 ){ level = 'A' ; }else if (maxScore - score <= 20 ){ level = 'B' ; }else if (maxScore - score <= 30 ){ level = 'C' ; }else { level = 'D' ; } System.out.println("student-" + i + " score is " + score + ",level is " + level); } } }
JUnit单元测试 步骤 : 1.选中当前工程 - 右键选择:build path - add libraries - JUnit 4
- 下一步 2.创建Java类,进行单元测试。 此时的Java类要求: ①此类是public
的 ②此类提供公共的无参的构造器 3.此类中声明单元测试方法。 此时的单元测试方法:方法的权限是public
,没有返回值,没有形参 4.此单元测试方法上需要声明注解:@Test
,并在单元测试类中导入:import org.junit.Test;
5.声明好单元测试方法以后,就可以在方法体内测试相关的代码。 6.写完代码以后,左键双击单元测试方法名,右键:run as - JUnit Test
说明 : 1.如果执行结果没有任何异常:绿条 2.如果执行结果出现异常:红条
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import java.util.Date;import org.junit.Test;public class JUnitTest { int num = 10 ; @Test public void testEquals () { String s1 = "MM" ; String s2 = "MM" ; System.out.println(s1.equals(s2)); System.out.println(num); show(); } public void show () { num = 20 ; System.out.println("show()...." ); } @Test public void testToString () { String s2 = "MM" ; System.out.println(s2.toString()); } }