------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
继承:
1.当一个类中包含了另一个类的所有变量个方法时,但另一个类中不包含另一个类的所有变量和方法时,表示范围比较小的就可以作为另一个的父类。
集合表示:A属于B,B不属于A,A就可以作为B的父类,B继承A
2.当只是为了获取其他类的功能的时候,不能为了简化代码而继承。
3.必须是类与类之间的所属关系才可以继承,所属关系看前面集合
继承的特点:
1.不支持多继承,只支持单继承:
多继承的话容易产生安全隐患:
class A{
Demo{
System.out.println("a");
}
}
class B{
Demo{
System.out.println("b");
}
}
class C extends A,B{
C c = new c();
c.Demo();
}class person{
int age;
String name;
}
class student extends person{
void study(){
System.out.println("good student");
}
}
class work extends person{
void work(){
System.out.println("good worker");
}
}
public class extendsDemo{
public static void main(String[] args){
student s = new student();
s.name = "Archer";
s.age = 21;
System.out.println("age:"+age+"name:"+name);
s.work();
work w = new work();
w.work();
}
}
java支持多层继承。也就是一个继承体系子类与父类出现后,类中的成员都有哪些特点:
1.成员变量
当子父类中出现一样的属性时,子类类型的对象,调用该属性,值是子类的属性值
class student{
int num = 4;
study(){
System.out.println("good study");
}
show(){
System.out.println(num);
}
}
class smallstudent extends student{
int num = 5;
show(){
System.out.println(num);
}
}
public class extendsDemo{
public static void main(String args[]){
new smallstudent().show();//输出的值为5,不是4
}
} 如果想要调用父类中的属性值,需要使用关键字superclass student{
int num = 4;
study(){
System.out.println("good study");
}
show(){
System.out.println(num);
}
}
class smallstudent extends student{
int num = 5;
show(){
System.out.println(super.num);//输出的是父类的值,也就是4
}
}
public class extendsDemo{
public static void main(String args[]){
new smallstudent().show();//输出的值为4
}
}class student{
student(){
System.out.println("student");
}
}
class smallstudent extends student{
smallstudent(){
System.out.println("smallstudent");
}
}
public class extendsDemo{
public static void main(String args[]){
smallstudent s = new smallstudent();
s.smallstudent();//输出是student
// smallstudent
}
}
所以子类构造函数实例化的过程中是先访问父类的构造函数。
注意事项:
class student{
student(){
System.out.println("student");
}
student(int n){
System.out.println("4");
}
}
class smallstudent extends student{
smallstudent(){
//这里是super关键字
System.out.println("smallstudent");
}
smallstudent(int m){
//这里可以是this关键字,也可以是super关键字
System.out.println("5");
}
}
public class extendsDemo{
public static void main(String args[]){
smallstudent s = new smallstudent();
s.smallstudent();
}
}
但是如果父类没有空参数的构造函数,这样就需要给子类手动在第一行添加super();了。
class student{
student(int n){
System.out.println("4");
}
}
class smallstudent extends student{
smallstudent(){
super(3);
System.out.println("smallstudent");
}
smallstudent(int m){
super(4);
System.out.println("5");
}
}
public class extendsDemo{
public static void main(String args[]){
smallstudent s = new smallstudent();
s.smallstudent();
}
}class student{
final showt(){
System.out.println("student");
}//这个方法在不可以被修改
}
class smallstudent extends student{
smallstudent(){
System.out.println("smallstudent");
}
}
public class extendsDemo{
public static void main(String args[]){
final PI = 3.14; //这个和c语言中的define有点像,和const更相似
smallstudent s = new smallstudent();
s.smallstudent();
}
}
抽象:
什么是抽象?
不具体,看不明白,抽象类表象体现
在不断抽取过程中,将共性内容中的方法申明抽取,但是方法是不一样的,没有抽取,这是抽取到的方法,并不是具体的,需要被指定关键字abstract所标示,申明为抽象方法。
抽象方法所在类一定要标示为抽象类,也就是说该类需要被abstract关键字所修饰
class student{
study(){
System.out.println("study");
}
}
class smallstudent extends student{
study(){
System.out.println("study english");
}
}
class bigstudent extends student{
study(){
System.out.println("study code");
}
}
ppublic class abstractDemo{
public static void main(String args[]){
}
}
这时候smallstudent和bigstudent中都用到了study方法,但是功能里面具体的实现不一样,再继承student类就没有什么意义,这时候就需要使用抽象类。
abstract class student{
abstract study();//这时候就只有方法,却没有方法体
}
class smallstudent extends student{
study(){
System.out.println("study english");
}
}
class bigstudent extends student{
study(){
System.out.println("study code");
}
}
ppublic class abstractDemo{
public static void main(String args[]){
}
}如果抽象类中包含多个抽象方法,子类必须对全部的抽象方法初始化,否则会出错。class Employee{
private String name;
private String id;
private int gongzi;
// 初始化
Employee(String name, String id, int gongzi){
this.name = name;
this.id = id;
thi.gongzi = gongzi;
}
public abstract work();//没有方法体
}
class Manage extends Employee{
private int bonus;
Manage((String name, String id, int gongzi, int bonus){
super(name,id,gongzi);
this.bonus = bonus;
}
public void work(){
System.ut.println("manage work");
}//区别
}
class pro extends Employee{
pro((String name, String id, int gongzi){
super(name,id,gongzi);
}
public void work(){
System.ut.println("pro work");
}
}原文地址:http://blog.csdn.net/orangeisnotapple/article/details/41492857