学习笔记九:面向对象编程(四)

没有风浪,便没有勇敢的弄潮儿;没有荆棘,也没有不屈的开拓者。 


本讲内容:this、super、包、修饰符、static

 

一、this

注意:this不能在类定义的外部使用,只能在类定义的方法中使用。

1、this.成员变量
当成员变量被局部变量隐藏时想使用成员变量,可以用this关键字来访问成员变量。

<span style="font-size:18px;">public class text {
	int i=1;
	int j=1;
	int k=1;
	static int x=1; //static 静态变量,是共享的,可以被任何一个对象访问
	private void test(int i) {
		int j=2;
		int x=2;
		System.out.println("i="+i);
		System.out.println("j="+j);
		System.out.println("k="+k);
		System.out.println("x="+x);
		
		System.out.println("this.i="+this.i);
		System.out.println("this.j="+this.j);
		System.out.println("this.k="+this.k);
		System.out.println("this.l="+this.x);
	}
	public static void main(String[] args) {
		text a=new text();
		a.test(2);
	}
}</span>
打印结果:

<span style="font-size:18px;">i=2          j=2         k=1          x=2  
this.i=1   this.j=1   this.k=1   this.l=1</span>



2、this()  构造函数
在构造方法中可以使用 this() 来引用另一个构造方法。

<span style="font-size:18px;">public class text {
	private int x=1108;
	public text() {
		this(1230);
	}
	public text(int x) {
		this.x=x;
	}
	public static void main(String[] args) {
		text a=new text();
		System.out.println(a.x);
	}
}</span>
打印结果:1230

注意:的是this()必须写在构造方法的第一行。


二、super

1、super.成员
当父类的成员变量被隐藏、成员方法被重写(覆盖),此时想使用父类的这些成员时就要用super关键字。

<span style="font-size:18px;">class Animals{
	int height = 80;
	public void pose() {
		System.out.println("Coo!");
	}
}
public class Dog extends Animals{
		int height=70;
		public void pose() {
			super.pose();
			System.out.println("Awesome!");
		}
		private void printHeight() {
			System.out.println(super.height);
			System.out.println(height);
		}
public static void main(String[] args) {
		Dog a=new Dog();
		a.pose();
		a.printHeight();
	}
}</span>
打印结果:

<span style="font-size:18px;">Coo!
Awesome!
80
70</span>


2、super() 父类构造函数

<span style="font-size:18px;">class Animals {
	public Animals() {
		System.out.println("动物类的构造函数");
	}
}
public class Dog extends Animals {
	public Dog() {
		super();//默认不用写
		System.out.println("狗类的构造函数");
	}
	public static void main(String[] args) {
		new Dog();
	}
}</span>

打印结果:

<span style="font-size:18px;"><span style="font-size:18px;">动物类的构造函数
狗类的构造函数</span></span>

首先调用的是main方法,main方法调用 new Dog() ,Dog()构造函数调用了一个默认的super(),super()方法就是父类的构造方法,以此类推最后调用了Object()构造方法。


3、带参数的的super()方法
在上面的例子里,我们看到编译器在你没有调用super()方法的时候,插入了一个默认的super()方法。可惜的是编译器并不会自动插入带参数的super(), 因此我们遇到这种情况就只能自己手工插入对super()的调用。

<span style="font-size:18px;">class Animals {
	int height=10;
	public Animals(int height) {
		this.height=height;
		System.out.println("动物类的有参构造函数");
	}
}
public class Dog extends Animals {
	public Dog() {
		super(20);
		System.out.println("狗类的构造函数");
	}
	public static void main(String[] args) {
		new Dog();
	}
}</span>

注意:构造函数只能用new、this() 和 super() 的方式来访问,是不能像方法一样写方法名访问的。


本讲就到这里,Take some time and enjoy it


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值