Java继承inheritance


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			
	}
}
如果你是一名技术人员可加我QQ 2651-0442-02,如果你是java技术人还可以加入QQ群 1784-9136-0
你将得到的不仅仅是技术的交流,还有职业机会,人生解惑.
首发地址:月小升博客https://java-er.com/blog/java-inheritance/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
此条目发表在 JAVA 分类目录,贴了 标签。将固定链接加入收藏夹。
既然来了,就评论一下,不会怀孕的

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">