博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
继承的概念及其基本用法
阅读量:38381 次
发布时间:2022-02-22

本文共 2923 字,大约阅读时间需要 9 分钟。

继承其实就是将父类的属性和方法,子类中可以使用

1.成员变量(属性)

公开的(public)和默认的(啥也不写)属性,子类是可以使用

私有的属性,子类是无法使用

2.成员方法(方法)

公开的(public)和默认的(啥也不写)方法,子类是可以使用的

私有的方法,子类是无法使用

3.构造方法

new Son1(); 尽管你是 new 的儿子类 但是会执行父类的构造方法

1.先执行父类的构造方法,然后再执行子类的构造方法

2.如果父类中没有无参构造方法,子类也不能有无参构造方法

继承至少得两个类:

语法格式:

class A {

    属性
    方法
class B  extends A {
    B就可以继承你的属性和方法
}

class Father1 {	String name1;}class Father2 extends Father1{	String name2;	}//Son  叫Father2  爹       Father2 叫 Father1 爹//Son 叫Father1  爷爷   多重继承class Son extends Father2 {	}public class Demo4 {	public static void main(String[] args) {			}}
class Father1 {	//公开的和默认的属性可以被子类继承的	public String name;//公开的属性	int age;//默认的属性		private int id;//私有化的		public Father1 () {			}		public Father1(String name, int age, int id) {				this.name = name;		this.age = age;		this.id = id;	}	//公开的方法	public  void  eat () {		System.out.println("吃红薯面");	}	//默认的方法	void  work () {				System.out.println("翻地球");	}	//私有话的方法  子类无法使用的e	private void smoking () {		System.out.println("抽旱烟");	}}class Son1 extends Father1{	public Son1 () {			}	public Son1(String name, int age, int id) {		super(name, age, id);//调用父类的有参构造方法		// TODO Auto-generated constructor stub	}		//mplicit super constructor Father1() is undefined. Must explicitly invoke another constructor	//父类中不存在,儿子的无参构造方法也不能存在//	public Son1 () {//		System.out.println("这个是子类的无参构造方法");//	}		  //总结:   子类的构造方法必须依靠父类的构造方法的形似来进行创建	}public class Demo10 {	public static void main(String[] args) {		Son1 son1 = new Son1();		son1.name = "小灰灰";//发现确实可以继承		son1.age = 17;//发现 默认的属性也是可以继承		//the field Father1.id is not visible  不可见 		//son1.id = 12;		son1.eat();		son1.work();		//son1.smoking();											}}

关于父类和子类的内存分析

 

重写(override)

重写的目的: 和继承有关 重写的是方法 子类是可以继承父类的非私有化的方法的

但是有的时候父类的方法需求满足不了子类的需求了,这个时候在子类中需要重写父类的方法

class Father3 {	public void eat () {		System.out.println("吃窝窝头");	}}class Son3 extends Father3{	/*	 * //重写:  就是把父类的方法重新写一遍,就是内容不一样	 * 父类的方法不能动,子类的方法重新写了一遍	 * 除了方法体中的内容不一样,其他都是一样的	 * 	 * 其他是啥:	 * 	1.方法的名字	 * 	2.方法返回值	 * 	3.方法的参数	 *///	public void eat (String name) {//		System.out.println(name + "吃大盘鸡");//	 }	@Override  //重写的严格限定 告知程序员 下面方法是重写的方法,不是自己独有的方法	public void eat() {		System.out.println("吃烤鸭");	}	}public class Demo5 {	public static void main(String[] args) {		Son3 son3 = new Son3();		son3.eat();//调用的父类的方法		//son3.eat("狗蛋");//调用的是子类独有的方法	}}

重写的规则:

1.必须有继承关系  

2.在子类中去重写父类方法
3.父类的方法必须是公开的或者默认的方法
4.在子类中重写父类的方法除了方法体不太一样,其他都一样(方法的返回值, 方法的名字 ,方法的参数)

重载(overload)

在Java中,同一个类中,有很多的方法,如果方法的名字一样,参数列表不一样,那么方法之间叫重载

重载的规则:

1.方法的重载必须写在同一个类中

2.方法的名字的一样
3.方法的参数列表一定不一样
4.方法的返回值可以一样也可以不一样
5.无参构造方法和有参构造方法也是方法的重载

import java.beans.IntrospectionException;class Person {		public void eat () {		System.out.println("吃饭");	}	public void eat (String name) {		System.out.println(name + "吃黄焖酥肉");	}	public void eat (String kind, int a) {		System.out.println(kind + "吃 "+a+"份黄焖酥肉");	}	public int eat (int a) {		return a;	}}public class Demo7 {	public static void main(String[] args) {			}}

转载地址:http://mvqcuy.baihongyu.com/

你可能感兴趣的文章
LeetCode019. Remove Nth Node From End of List
查看>>
LeetCode130. Surrounded Regions (思路及python解法)
查看>>
LeetCode412. Fizz Buzz(思路及python解法)
查看>>
LeetCode46. Permutations (思路及python解法)
查看>>
LeetCode347. Top K Frequent Elements(思路及python解法)
查看>>
LeetCode238. Product of Array Except Self(思路及python解法)
查看>>
LeetCode78. Subsets(思路及python解法)
查看>>
LeetCode230. Kth Smallest Element in a BST(思路及python解法)
查看>>
LeetCode48. Rotate Image(思路及python解法)
查看>>
LeetCode8. String to Integer (atoi)(思路及python解法)
查看>>
LeetCode11. Container With Most Water (思路及python解法)
查看>>
LeetCode17. Letter Combinations of a Phone Number(思路及python解法)
查看>>
LeetCode34. Find First and Last Position of Element in Sorted Array (思路及python解法)
查看>>
LeetCode49. Group Anagrams (思路及python解法)
查看>>
LeetCode41. First Missing Positive(思路及python解法)
查看>>
LeetCode50. Pow(x, n)(思路及python解法)
查看>>
LeetCode55. Jump Game(思路及python解法)
查看>>
LeetCode56. Merge Intervals(思路及python解法)
查看>>
LeetCode73. Set Matrix Zeroes(思路及python解法)
查看>>
LeetCode15. 3Sum(思路及python解法)
查看>>