一、多态性
1、通过name在对象数组中查找。代码:MyGroupDemo.java
class Group //是因为和Person.class Student.class在一个文件夹下面,所以就不用继承和导包哈?
{
static void print(Object table[])
{
if(table != null)
for(int i = 0; i < table.length; i++)
System.out.println(table[i].toString()); //运行时多态
}
static int indexOf(Person[] table, Person p) //我没有用(Object table[], Object obj)
{
for(int i = 0; i < table.length; i++)
if(p.name.equals(table[i].name))
return i;
return -1;
}
static Person oldest(Person[] table)
{
int max = 0;
for(int i = 0; i < table.length; i++)
{
if(table[i].getAge() > table[max].getAge())
max = i;
}
return table[max];
}
}
class MyGroupDemo
{
public static void main(String[] args)
{
Person[] table = {new Person("王一", new MyDate(1995, 5, 7)),
new Student("王二", new MyDate(1993, 3, 2), "计算机"),
new Student("王三", new MyDate(1990, 12, 9), "通讯")};
Group.print(table);
String s1 = "hei";
String s2 = "hei";
System.out.println(s1.equals(s2));
String name = "王二";
int index = Group.indexOf(table, new Person(name)); //查找一个名字就new一个Person,好浪费空间哦?
System.out.println("查找\"" + name + "\"结果:" + ((index != -1) ? table[index] : "未找到"));
System.out.println("年龄最大者是(" + Group.oldest(table) + ")");
Student.howMany();
}
}
二、抽象性
1、声明抽象类与抽象方法
- 抽象方法只有方法声明(以“;”结束),没有方法体。
2、抽象类的特点
- 一个抽象类可以不包含抽象方法,但是包含抽象方法的类必须被声明为抽象类。构造方法、静态成员方法不能被声明为抽象方法。
- 一个非抽象类必须实现从父类继承来的所有抽象方法
- 抽象类不能被实例化
3、闭合图形抽象类(代码:MyClosedFigureDemo.java)极其子类(Ellipse.class Rectangle.class)
abstract class ClosedFigure
{
String shape;
ClosedFigure()
{}
ClosedFigure(String shape)
{
this.shape = shape;
}
abstract double perimeter();
abstract double area();
void print()
{
System.out.println("一个" + this.shape + ", " + this.toString() + ", 周长为" + this.perimeter() + ", 面积为" + this.area());
}
}
class Ellipse extends ClosedFigure
{
//super.shape = "tuo_yuan"; //为什么有错呢?
double radius_a, radius_b;
Ellipse(double radius_a, double radius_b)
{
super("椭圆");
this.radius_a = radius_a;
this.radius_b = radius_b;
}
Ellipse()
{
this(0,0);
}
public String toString()
{
return "a轴半径" + radius_a + ", b轴半径" + radius_b;
}
double perimeter()
{
return Math.PI * (radius_a + radius_b);
}
double area()
{
return Math.PI * radius_a * radius_b;
}
}
//跟Ellipse类思想一样的
class Rectangle extends ClosedFigure
{
double length, width;
Rectangle()
{
this(0, 0);
}
Rectangle(double length, double width)
{
super("矩形");
this.length = length;
this.width = width;
}
double perimeter()
{
return (this.length + this.width) * 2;
}
double area()
{
return this.length * this.width;
}
public String toString()
{
return "长" + this.length + ", 宽" + this.width;
}
}
class MyClosedFigureDemo
{
public static void main(String[] args)
{
//ClosedFigure a = new ClosedFigure(); // 错误: ClosedFigure是抽象的; 无法实例化
ClosedFigure g = new Ellipse(10, 20);
g.print();
g = new Rectangle(10, 20);
g.print();
}
}
4、最终类
- 抽象类不能被声明为最终类
- 最终类不能被继承,即不能声明最终类的子类
- 最终方法不能被子类覆盖
- 最终类中包含的都是最终方法。非最终类也可以包含最终方法
三、接口
1、声明接口
- 使用关键字interfce声明接口
- 接口中不能包含构造方法,因为构造方法不能是抽象的
- 接口的访问控制权限是public或默认。 为什么呢?
- 接口中的成员变量都是常量,默认修饰符为public static final,声明时必须赋值;不能声明实例成员变量
- 接口中的方法都是抽象的成员方法,默认修饰符为public abstract,不能声明为static
- 接口没有任何具体实现,也就不能创建实例
2、声明实现接口的类
- 一个类可以实现多个接口,多个接口之间用逗号分隔
- 实现接口的非抽象类必须实现(覆盖)所有指定接口中的所有抽象方法,方法的参数列表必须相同
3、可计算面积接口及实现该接口的矩形类。代码:MyRectangleInterfaceDemo.java
interface Area
{
abstract double area();
}
class Rectangle implements Area
{
protected double length, width;
Rectangle()
{
this(0, 0);
}
Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
Rectangle(double width)
{
this(width, width);
}
//为什么前面的访问权限必须加个public才行呀?接口里面的都是defaul
public double area() //实现Area接口中的抽象方法
{
return this.length * this.width;
}
double perimeter()
{
return (this.length + this.width) * 2;
}
public String toString()
{
return "一个矩形:长" + this.length + ", 宽" + this.width + ", 周长为" + this.perimeter() + ", 面积为" + this.area();
}
void print()
{}
}
class MyRectangleInterfaceDemo
{
public static void main(String[] args)
{
Rectangle r = new Rectangle(10,20);
System.out.println(r);
Rectangle r1 = new Rectangle(10);
System.out.println(r1);
}
}
4、可计算体积接口及实现该接口的长方体类。代码:MyCuboidInterfaceDemo.java
interface Volume
{
abstract double volume();
}
class Cuboid extends Rectangle implements Volume
{
protected double length;
protected double width;
protected double height;
Cuboid()
{
this(0, 0, 0);
}
Cuboid(double length, double width, double height)
{
super(length, width);
this.height = height;
}
Cuboid(double width)
{
this(width, width, width);
}
/*
public double perimeter()
{
//Cuboid类不需要继承来的perimeter()方法,但无法删除它,只能覆盖。但是我不要这部分,也没报错呀?
return 0;
} */
public double area()
{
//return 2 * (super.length * super.width + super.length * this.height + super.width * this.height);
return super.perimeter() * this.height + 2 * super.area();
}
public double volume() //为什么非要加public呢?
{
return super.area() * height;
}
public String toString()
{
return "一个矩形:长" + super.length + ", 宽" + super.width + ", 高" + this.height + ", 表面积" + this.area() + ", 体积" + this.volume();
}
}
class MyCuboidInterfaceDemo
{
public static void main(String[] args)
{
Cuboid c = new Cuboid(1, 2, 3);
System.out.println(c);
}
}
5、球类实现多个接口。代码:MyGlobleDemo.java
class Globle implements Area, Volume
{
protected double radius;
Globle()
{
this(0);
}
Globle(double radius)
{
this.radius = radius;
}
public double area()
{
return 4 * Math.PI * radius * radius;
}
public double perimeter()
{
return 0; //虽然没用到,但是implenments了Area,就要覆盖它的perimeter()
}
public double volume() //为什么老是要加个public呢?
{
return (4.0 / 3) * Math.PI * radius * radius * radius;
}
public String toString()
{
return "一个球,半径" + this.radius + ", 表面积为" + this.area() + ", 体积为" + this.volume();
}
}
class MyGlobleDemo
{
public static void main(String[] args)
{
Globle g = new Globle(10);
System.out.println(g);
}
}
6、接口的继承是多继承,即一个接口可以有多个父接口。例如:
public interface AreaVolume extends Area, Volume
public class Globle implements AreaVolume