您的位置:首页 > 编程语言 > Java开发

JAVA基础学习20180106-继承上

2018-01-07 10:26 316 查看
1.继承的概念

(1)一种类与类之间的关系

(2)使用已存在的类的定义作为基础建立新类

(3)新类的定义可以增加新的数据或新的功能,也可以用父类的功能,但不能选择性的继承父类

(4)满足“A is a B”的关系

2.继承的特点

(1)利于代码复用

(2)缩短开发周期

3.语法:

(1)使用extends实现继承

class Dog extends Animal{

//子类特有的属性和方法

}

(2)单一继承,只能有一个父类

4.继承后的初始化顺序

父类静态成员子类静态成员父类对象构造子类对象构造

5.super

(1)子类的构造过程中必须调用其父类的构造方法,默认调用无参的构造方法。

(2)如果子类构造方法中既没有显式标注,而父类又没有无参的构造方法,则编译出错。

(3)使用super调用父类指定构造方法,必须在子类的构造方法第一行。

6.super和this

this: 当前类对象的引用

访问当前类的成员方法

访问当前类的成员属性

访问当前类的构造方法

不能在静态方法中使用

super: 父类对象的引用

访问父类的成员方法

访问父类的成员属性

访问父类的构造方法

不能在静态方法中使用

构造方法调用时,super和this不能同时出现

7.方法重写和方法重载

方法重写:

(1)在满足继承关系的子类中

(2)方法名、参数个数、顺序、返回值与父类相同

(3)访问修饰符的限定范围大于等于父类方法

方法重载:

(1)在同一个类中

(2)方法名相同

(3)参数个数、顺序、类型不同

(4)返回值类型、访问修饰符任意

8.访问修饰符的访问范围和限定能力



9.代码案例

package com.immoc.animal;

public class Animal {
/*private:只允许在本类中进行访问
*public:允许在任意位置访问
*protected:允许在当前类、同包子类/非子类、跨包子类调用,跨包非子类不允许
*默认:允许在当前类、同包子类/非子类调用,跨包子类/非子类不允许调用
* */
private String name="妮妮";//昵称
protected int month=2;//月份
String species="动物";//品种
public int temp=15;

public static int st2=23;
private static int st1=22;

static{
System.out.println("我是父类的静态代码块");
}

{
System.out.println("我是父类的构造代码块");
}

//父类的构造不允许被继承、不允许被重写,但是会影响子类对象的实例化
public Animal(){
System.out.println("我是父类的无参构造方法");
}

public Animal (String name,int month){
System.out.println("我是父类的带参构造方法");
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public String getSpecies() {
return species;
}

public void setSpecies(String species) {
this.species = species;
}

//吃东西
public void eat(){
System.out.println(this.getName()+"在吃东西");
}

public void eat(String name){
System.out.println(name+"在吃东西");
}
}

package com.immoc.animal;

public class Cat extends Animal{
private double weight=11.24;//体重
//  public int temp=300;
public static int st3=44;

static{
System.out.println("我是子类的静态代码块");
}

{
System.out.println("我是子类的构造代码块");
}

public Cat(){
//      Animal temp=new Animal();
//      temp.name;
//      this.temp=12;
//      this.month=23;
//      this.species="";
System.out.println("我是子类的无参构造方法");
}

public Cat(String name,int month){
/* 子类构造默认调用父类无参构造方法
* 可以通过super()调用父类允许被访问的其他构造方法
* super()必须放在子类构造方法有效代码第一行
* */
super(name, month);
//      this();
System.out.println("我是子类的带参构造方法");
}

public static void say(){
//      this.weight=20;
//      super.name="aa";
}

public double getWeight() {
return weight;
}

public void setWeight(double weight) {
this.weight = weight;
}

//跑动的方法
public void run(){
eat();
System.out.println(this.getName()+"是一只"+this.getSpecies()+",它在快乐的奔跑");
}
}

package com.immoc.animal;

public class Dog extends Animal {
private String sex;//性别

public Dog(){

}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

//睡觉的方法
public void sleep(){
super.eat();//调用的哪个eat();
super.species="犬科";
System.out.println(this.getName()+"现在"+this.getMonth()+"个月大,它正在睡觉~~");
}

/*
* 方法重载:
* 1、在同一个类中
* 2、方法名相同,参数列表不通(参数的顺序、个数、类型)
* 3、方法返回值、访问修饰符任意
* 4、与方法的参数名无关
*
* 方法重写
* 1、有继承关系的子类中
* 2、方法名相同,参数列表相同(参数顺序、个数、类型),方法返回值相同
* 3、访问修饰符,访问范围需要大于等于父类的访问范围
* 4、与方法的参数名无关
* */
//  private String sleep(String name){
//      return "";
//  }
//  public void sleep(String name,int month){
//
//  }
//  public void sleep(int month,String name){
//
//  }
//  public void sleep(int name,String month){
//
//  }

//子类重写父类吃东西的方法
public void eat(){
System.out.println(this.getName()+"最近没有食欲~~");
}

public void eat(String month){
System.out.println(month+"最近没有食欲~~");
}
}

package com.imooc.test;

import com.immoc.animal.Animal;
import com.immoc.animal.Cat;
import com.immoc.animal.Dog;

public class Test {

public static void main(String[] args){
//      Cat one=new Cat();
//      one.setName("花花");
//      one.setSpecies("中华田园猫");
//      one.eat();
//      one.run();
//      System.out.println(one.temp);
//      System.out.println("==========================");
Dog two=new Dog();
two.setName("妞妞");
two.setMonth(1);
//      two.eat();
two.sleep();
//      System.out.println("==========================");
//      two.eat("凡凡");
//      System.out.println("==========================");
//      Animal three=new Animal();
//      three.month=2;
//      three.species="";
//      three.name;
//      three.run();
//      three.sleep();
}

}

package com.imooc.test;

import com.immoc.animal.Cat;

public class TestTwo {

public static void main(String[] args){
//      Cat one=new Cat();
Cat one=new Cat("花花",2);
System.out.println(one.temp);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java