java inheritance_Java继承inheritance

发表于 2012-10-23 03:56:22 by 月小升

Java继承

Java inheritance

我们可以把继承看成是儿子,继承了父亲的一些特点,但是儿子可以有一些改变。

java里儿子继承了父亲的公共属性 和公共方法,儿子可以修改这些属性和方法。儿子修改父亲的公共属性,我们叫重载。没有修改的叫继承。

We can think of that their children inherit their father, the child can have the father of all public properties and public methods, you can also change these properties and methods. When the children change his father’s property, we call it overide. Does not change is called inheritance.

下面看一个汽车和蓝色汽车的例子。

father class

/**

java-er.com

learn java is so easy

*/

import java.util.*;

public class Car // Car is a class

{

public int wheel = 4; // wheel,car's property

public void run(){ // a method , car can run

System.out.println("car run");

}

public void stop(){ // a method , car can run

System.out.println("car stop");

}

public static void main(String[] args){

Car c1 = new Car(); //a new object

c1.run();

Car c2 = new Car();// second object

c2.stop();

//there are two car

// first run, second stop.

/*

Car is a class, c1 is an Object,c2 is other Object.

*/

}

}

child class

/**

java-er.com

learn java is so easy

*/

import java.util.*;

public class BlueCar extends Car// Car is a father class,Blue Car is a child class.

{

public void MYColor(){

System.out.println("my color is blue");

}

public void stop(){ // overide

System.out.println("I can stop!");

}

public static void main(String[] args){

BlueCar c1 = new BlueCar(); //a new object

c1.run(); // child has father's method and property

System.out.println("c1 wheel is " + c1.wheel);

c1.stop(); // child can change the method what father has. we call it overide in java.

c1.MYColor(); // child has same method what father has not

}

}

This entry was posted in JAVA and tagged java基础教程. Bookmark the permalink.

月小升QQ 2651044202, 技术交流QQ群 178491360

首发地址:月小升博客 – https://java-er.com/blog/java-inheritance/

无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值