/*
多态:父类的引用指向子类的对象。
当两个类中有同一个方法(覆盖)时会优先调用子类
*/
Demo1。
package pc2;
class Father
{
public void func1()
{
func2();
}
public void func2()
{
System.out.println("AAA");
}
}
class Son extends Father
{
public void func1(int i)
{
System.out.println("BBB");
}
public void func2()
{
System.out.println("CCC");
}
}
public class Duotai {
public static void main(String[] args) {
// TODO Auto-generated method stub
Father child=new Son();
child.func1();child.func2();
/*child.func1(22);不能通过编译父类里没有func1(int i)*/
}
}
输出 CCC
CCC
Demo2.
package pc2;
class Vehicle
{
public void startUp()
{
System.out.println("调用了Vehicle类的startUp方法");
}
}
class Car extends Vehicle
{
public void startUp()
{
System.out.println("调用了Car类的startUp方法");
}
}
public class Sample4_5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car c=new Car();
c.startUp();//
Vehicle v=new Car();
v.startUp();
}
}
输出:调用了Car类的startUp方法
调用了Car类的startUp方法
Demo3
package pc3_memory;
class Vehicle
{
void startUp()
{
System.out.println("一般汽车的启动方法");
}
String s="I'm OK!";
}
class Car extends Vehicle
{
void startUp()
{
System.out.println("小轿车的启动方法");
}
String s="Maybe you are right";
}
public class Sample4_5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//动态链接时方法与变量的区别
Vehicle v=new Car();
v.startUp();
System.out.println(v.s);
System.out.println(((Car)v).s);//强制类型转化
}
}
输出:小轿车的启动方法
I'm OK!
Maybe you are right
Demo4
/*
对象与数组的关系
*/
Deno1
package pc2;
public class Reference {
public static void main(String args[])
{
Person students[];
students=new Person[2];
// System.out.println( students[1].age);
System.out.println("length "+students.length);
Person zhang=new Person();//定义并且实例化一个叫zhang的Person对象
zhang.age=15;
zhang.height=200;
Person lee=new Person();
lee.age=17;
lee.height=204;
students[0]=zhang;//把对象赋给Person类型的数组
students[1]=lee;
lee.info();//最后两条语句功能相同
students[1].info();
}
}
class Person {
public int age;
public double height;
public void info()
{
System.out.println("my age is:"+age+"my height is:"+height);
}
}
/*
length2
my age is:17my height is:204.0
my age is:17my height is:204.0
*/
Demo5
String s2="Monday";与
String s2=new String("Monday");的区别
package pc3_memory;
public class StringTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="Monday";
String s2="Monday";
//String s2=new String("Monday");依次按照上下隐藏s2
if(s1==s2)
System.out.println("s1==s2");
else
System.out.println("s1!=s2");
if(s1.equals(s2))
System.out.println("s1.equals(s2)");
else
System.out.println("!s1.equals(s2)");
}
}
/*在堆里面创建好了字符串1过后,开创建s2时会先在堆里面(字符前缓冲池)判断有没有与s1对象相同值的
字符串,若有则s2会直接的引用s1.
如果使用new操作符,则表明需要一个新的,不要旧的。一个新的值相同的对象被创建,在缓冲池的不同位置
。就好比如一个人在河里游泳,而另一个人在岸边散步*/
/*
输出
s1==s2
s1.equals(s2)
*/
隐藏上面的:输出
s1!=s2
s1.equals(s2)
Demo6接口,内部类
内部类
package pc2;
interface ComputerWeight//定义了一个接口实现计算重量的功能
{
public double ComputeWeight() ;
//接口没有构造函数,因为构造函数不能继承,Java是纯虚的,抽象类可以有构造函数
}
class Television implements ComputerWeight//以下三个类实现了接口的功能,通过在类体里实现接口的方法
{//计算电视机的重量
Television(double a)
{
this.a=a;
}
public double ComputeWeight()
{
return a;
}
double a;
}
class Computer implements ComputerWeight
{//计算电脑的重量
Computer(double a)
{
this.b=a;
}
public double ComputeWeight()
{
return b;
}
double b;
}
class WashMachine implements ComputerWeight
{//计算洗衣机的重量
WashMachine(double a)
{
this.c=a;
}
double c;
public double ComputeWeight()
{
return c;
}
}
class Car//定义汽车类,实现将不同的接口实现的功能相加
{
ComputerWeight goods1;//表现出like-a
ComputerWeight goods2;
ComputerWeight goods3;
Car(ComputerWeight goods1,ComputerWeight goods2,ComputerWeight goods3)
{
this.goods1=goods1;
this.goods2=goods2;
this.goods3=goods3;
}
public double getTotalWeights()
{
return goods1.ComputeWeight()+goods2.ComputeWeight()+goods3.ComputeWeight();
}
}
public class InterTest {
public static void main(String args[])
{ ComputerWeight goodsOne=new Television(93);/*接口类型的引用
指向一个实现该接口的对象,这是Java里的一种多态现象。
Java里的接口不能被实例化,通过接口来调用方法,屏蔽掉具体方法的实现。
这就是接口回调,也叫做面向接口的编程
*/
ComputerWeight goodsTwo=new Computer(32);
ComputerWeight goodsThree =new WashMachine(45);
Car car=new Car(goodsOne, goodsTwo, goodsThree);
System.out.println(car.getTotalWeights());
System.out.println(goodsOne.ComputeWeight());
System.out.println(goodsTwo.ComputeWeight());
System.out.println(goodsThree.ComputeWeight());
}
}
输出:
170.0
93.0
32.0
45.0