Prohibitedpackage name: java 禁止使用java做包名
面向对象的深入了解
1.两种多态
面向对象的三大特点:继承,封装和多态
Animal a2=new Dog();//引用一个父类的子类,狗也是动动物“父类中的引用指向派生类子类的对象”
a2.eat(); //调用Dog时如果子类没有重写就会调用父类中的eat,重写了就用自己的
重载和重写(覆盖)overloading(重载),overriding(重写)
Alt+Shift+S – override //快捷键引用父类中的方法
一个父类引用指向子类对象的例子。
数组,array,可以存放多个同一种类型的变量
a[0]=new Animal();//数组的下标,或者索引 index,从0开始
循环:反复做某事 四组循环
Animal a[]=new Animal[4];
a[0]=new Animal();//数组的下标,或者索引 index,从0开始
a[1]=new Dog();
a[2]=new Cat();
a[3]=new person();
for(int i=0;i<=3;i++)
{
a[i].eat();
}
类型的强制转换
定义好person中的sd
Animal a=new person();//将a当做Animal不是person
person p=(person)a;//蒋a给p方法的强制转换
p.sd();
2.传值与传址
简单类型的变量传值 byte,short,int,long / float,double / boolean/ char
int a=100;
int b=a;//把a复制给b,复制完后就断开了 传值
a=20;
System.out.println(b);
结果是100。
引用类型的变量传址 String,对象
person x=new person();
x.age=30;
person y=x;//把x的地址传给了y,x.y都指向同一个地址,地址中的数变了所以他也变 传址
x.age=60;
System.out.println(y.age);
结果是60。
3.接口 interface
接口 有点类似于 继承 中的父类
可以通过接口来实现子类继承多个父类
助教
接口中只能有抽象方法 abstract method
public interface Student {
public void study();//抽象类方法,只有方法没有方法的实现
implements(实现)extends(继承)
Assist a=new Assist();
a.teche();
a.study();
Teacher t=new Assist();
t.teche();
Assist s=(Assist)t;//接口的强制转换
s.study();
The type Assist must implement the inheritedabstract method Teacher.teach()
输出结果
eclipse 自动提示
Window-> preferences -> Java -> Editor -> Content assist ->Auto-Activation
.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
4.抽象类
abstract class 包含抽象方法的类,叫做抽象类。
接口可以理解成纯抽象类。
什么是抽象??
a. 编写类的时候,其中包含了抽象方法abstract
public abstract class Can {
public void name(){
System.out.println("我的名字叫大江");
}
public abstract void hello();
}
Thismethod requires a body instead of a semicolon
这个方法需要一个方法体不要用分号;
The abstract method study in type Person canonly be defined by an abstract class
b.继承抽象类后,其父类中的抽象方法没有实现。实现父类中的hello
public class Xuesheng extends Can {
public void hello() {
// TODO Auto-generated method stub
System.out.println("学习");//重新实例化方法
}
抽象类不能实例化
Cannot instantiate the type Person
抽象类不能够实例化,不能创建对象
The type Student must implement the inheritedabstract method Person.study()
必须实现他的方法
b. 实现接口的时候,其中有的抽象方法没有实现。
只实现了一个也叫抽象类
public abstract class Xiaozhang implements Xuexiao,Shangxue {
public void shangxue() {
// TODO Auto-generated method stub
System.out.print("去上学");
}
5.内部类
类中又包含类,不是重点,只做了解,需要时可以参考以下文档。
http://www.360doc.com/showWeb/0/0/412191736.aspx