this关键字、方法的参数、多类合作以及继承

本文介绍了Java编程中this关键字的用法,包括在构造方法中引用当前对象、调用属性和方法。同时,文章探讨了类对象作为方法参数的应用,以及多个类之间的协作关系,如人类喂养狗。还详细讲解了Java中的继承概念,如何通过extends关键字实现子类对父类属性和方法的继承,并强调了访问控制在继承中的影响。
摘要由CSDN通过智能技术生成

1.this关键字

字面意思: 这个

1.一般写在实体类中。 表示的是当前的对象

可以使用 this调用属性和方法

2.this也可以调用构造方法(很少用)

注意事项:

1.一定是在构造方法调用另外一个构造方法

2.在构造方法中调用其他的构造方法的时候,构造调用时候一定写在首行

class Person {
	
	String name;
	int age;
	
	public Person () {
		System.out.println("Line 9:  "+ this);//代表是当前的对象
		this.name = "狗蛋";//this可以调用属性
	}
	
	public void  eat () {
		//this可以调用方法
		this.test();
	}
	public void test () {
		System.out.println("嘻嘻");
	}
	
}
public class Demo1 {
	public static void main(String[] args) {
		Person person1 = new Person();
		System.out.println(person1);//对象的内存地址  15db9742
		System.out.println(person1.name);
		person1.eat();
		
		Person person2 = new Person();
		System.out.println(person2);//对象的内存地址6d06d69c
		System.out.println(person2.name);
	}
}

2.方法的参数是一个类对象

方法的参数可以是八大基本数据类型 , String , 数组

类对象也是作为方法的参数

人喂狗,狗吃饭

class People {
	//一个类对象可以作为一个方法的参数
	public void feed (Dog1 dog1) {//这个是喂的方法
		dog1.eat();
	}
}
class Dog1 {
	public void eat () {
		System.out.println("狗去啃大骨头");
	}
}
public class Demo4 {
	public static void main(String[] args) {
		People people = new People();
		Dog1 dog1 = new Dog1();
		people.feed(dog1);
	}
}

人生孩子,孩子去玩玩具 ,玩具被玩(在玩具类中打印玩具被玩)

class Adult {//成年人类
	
	public void birth (Child child) {
		Toy toy2 = new Toy();
		child.playToy(toy2);
	}
}
class Child {
	public void playToy (Toy toy) {
		toy.bePlay();
	}
}
class Toy {
	public void bePlay () {//玩具被玩
		System.out.println("被玩,很开心");
	}
}
public class Demo6 {
	public static void main(String[] args) {
		Adult adult = new Adult();
		Child child= new Child();
		adult.birth(child);
	}
}

3.多类合作

一个类对象可以作为另外一个类的属性

class RenLei {//人类
	
	String name;
	int age;
	BigDog bigDog;//bigDog这个对象带了三个数据   牛彩云    3   黑色
}
class BigDog {
	String name;
	int age;
	String color;
	
}
public class Demo8 {
	public static void main(String[] args) {
		
		BigDog bd = new BigDog();
		bd.name = "牛彩云";
		bd.age = 3;
		bd.color = "黑色";
		RenLei renLei = new RenLei();
		renLei.name = "二贝";
		renLei.age = 38;
		renLei.bigDog = bd;
		System.out.println(renLei.name);
		System.out.println(renLei.age);
		//人的狗 狗的名字
		System.out.println(renLei.bigDog);//内存地址
		System.out.println(renLei.bigDog.name);//  牛彩云
		System.out.println(renLei.bigDog.age);//3
		System.out.println(renLei.bigDog.color);//黑色
		
		
	}
}

4. 继承【重点】

4.1生活中继承

子承父业

龙生龙 凤生凤 老鼠的儿子会打洞

4.2Java中继承

继承至少要有两个类:

语法格式:

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

class Father {
	//属性
	String name;
	int age;
	//行为
	public void eat () {
		System.out.println("吃窝窝头");
	}
	
}
class Son extends Father{//想让Son 去继承Father类
	
	
}
public class Demo9 {
	public static void main(String[] args) {
		Son son = new Son();
		son.name = "大头";//儿子类中可以使用父类的属性
		son.age = 15;
		System.out.println(son.name);
		System.out.println(son.age);
		son.eat();
	}
}

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

1.成员变量(属性)

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

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

2.成员方法(方法)

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

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

3.构造方法

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值