"this"
1、可以表示在构造器中引用满足指定参数类型的其他构造器,只能引用一个构造方法且必须位于开始!
this(a,b)表示调用另外一个构造函数。这里面的this就是一个特殊语法,不是变量,没有什么类型。
2、可以在一个类的非static成员内部使用,表示当前这个对象。
此时,this就是一个final的普通变量,它有静态类型,就是这个类本身;它有动态类型,就是当前这个对象的类型。可以对它调用成员 函数,把它传递给别的函数,等等。只要一个C类型的final变量可以出现的地方,它就可以出现。
3、表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!
例子:
class A {
int i=10;
private int a,b;
public String color="绿"; //全局变量
public A(){
this(11);
}
public A(int a){
this.a=a+1;
System.out.println("a:"+a);
System.out.println("this.a:"+this.a);
}
public void method(){
String color="红"; //局部变量
i=15; //改变全局变量i的值
System.out.println("当前对象this调用i:"+this.i);
System.out.println("new 一个新的对象调用i:"+new A().i);
System.out.println("用关键字this调用全局变量color:"+this.color);
System.out.println("直接使用局部变量i:"+color);
}
public static void main(String[] args){
new A().method();
}
}
结果:
4、为什么静态的方法中不能使用this关键字(在static方法的内部不能调用非静态方法)
this关键字指“当前对象实例”,然而main方法是静态的,它是程序的入口,在程序编译的时候,它会自动地被JVM加载到内存中。
注意,在这个时侯,程序还没有实例化对象,所以,自然不能在main方法里使用非静态的this了。
注:如果你传递一个对象的引用到静态方法里(静态方法可以创建其自身对象),然后通过这个引用(和this效果相同),你就可以调用非静态方法和访问非静态数据成员了。
例子:
NoStatic 方法public class NoStatic {
public void outPut(){
System.out.println("非静态输出");
}
}
这个是主类,test方法是static静态方法,调用了outPut的非静态的方法。
传递一个对象的引用,这里将NoStatic 的引用作为参数传递到test
public class Static {
public static void main(String[] args) {
NoStatic ns =new NoStatic();
Static.test(ns);
}
public static void test(NoStatic ns){
ns.outPut(); //静态方法里调用了非静态(特例)
}
}
"super"
1、super();表示调用父类的构造方法,只是调用方法,不构造对象。是一个特殊语法,不是变量,没有什么类型
2、super.fun();表示调用父类的某方法,在继承的时候这个方法是肯定被继承下来的。
如果fun()这个方法在子类没有重写,那么super.fun();实际上跟直接调用fun();是一样的。如果被重写了,那么super.fun();就是重写前的,fun();就是重写后的。也可以在一个类的非static成员内部使用。比如super.method()。
3。super的另外一个作用是调用父类的protected函数。只有通过"super",我们才能操作父类的protected成员。
4,java里在了类中用super调用父类构造函数时,为什么调用语句必须是子类的第一条语句啊?
如果想用super继承父类构造的方法但是没有放在第一行的话 ,那么在super之前的语句肯定是为了满足想要完成某些行为的语句但是又用了super继承父类的构造方法那么以前所做的修改就都回到以前了 ,也就是说又成了父类的构造方法了,比如 :class Father { public Father() {String int age=0;} } class Son extends Father { public Son() {String ; super();} } //这样子的话就会把Son里面的name又变成了null 没有达到想要的效果
附:子类继承父类的所有成员变量和成员方法,但不继承父类的构造方法。
父类的缺省构造函数在子类初始化时会被自动调用来完成相应的父类对象初始化,在子类的构造方法中,可使用super(参数列表)调用父类的构造方法。
分享到:
相关推荐
Title : Super PI Ver1.1e (calculation of pi up to 33.55 million digits) Keywords: PI MATH WINDOWS In August 1995, the calculation of pi up to 4,294,960,000 decimal digits was succeeded by using a...
This perfect publication about MUSIC for multidimensional spectral estimation: stability and super-resolution.
fast and robust multi-frame super-resolution algorithm. The resource requirements of the proposed architecture scale linearly with the targeted image quality, making it ideally suited for a variety of...
Cross-lingual Entity Linking (XEL) aims to ground entity mentions written in any language to an English Knowledge Base (KB)...scenarios, and shows the value of joint supervision in low-resource settings
This paper propose an alternate approach using L1 norm minimization and robust regularization based on a bilateral prior to deal with different data and noise models.
task and a lower level super-resolution process, in which we incorporate reconstruction-based super-resolution algorithms, single-image enhancement, and image/video classification into a single ...
In this dissertation we propose priors and learning based methods for super- resolution and other video processing applications. We also propose efficient al- gorithms for global motion estimation and ...
https://forum.xda-developers.com/android/software-hacking/super-adb-tool-reborn-satr-command-lines-t3221994 先科普下什么叫做ADB,仅供新手...Show online changelog of this tool (显示本工具的changelog)
Benefit from your personally configured super computer which is a power efficient, low-cost, and highly scalable super computer Write your own cluster software with the help of practical examples ...
The edge and corner ... The common PDE framework is applied to several other image processing problems, including image denoising, deringing, deblocking, inpainting, and super-resolution reconstruction.
Super resolution (SR) methods typically assume that the low-resolution (LR) image was downscaled from the unknown high-resolution (HR) image by a fixed ‘ideal’ downscaling kernel (e.g. Bicubic ...
(Eds.) Counselor education and supervision: Readings in theory, practive and research. Springfield, Ill.: Charles C. Thomas, 1972, 421 p., [dollar]14.75 274 BOOK BRIEFS SELIGMAN, M. and BALDWIN, ...
Chapter 1, Getting Started ... However, in this chapter, to ensure we use our new superpowers responsibly, we'll do a code review and consider how to use development tools to test code before deployment.
Expiry Time and Garbage Collection 过期时间和垃圾收集,这里主要设置缓存文件过期时间,和多久删除一次过期的缓存文件。个人建议:一般缓存时间设置为1天(86400)左右,缓存删除时间为1个小时一次。 Accepted ...
This handy, one–stop guide catches you up on XHTML basics and CSS fundamentals. You′ll learn how to work with Positionable CSS to create floating elements, margins, and multi–column layouts, and ...
This book will suit a wide audience of OpenGL and Windows programmers. Windows programmers wanting to learn about 3D graphics and how to implement them using OpenGL will find what they need. So will...
For the improvement of reliability, safety and efficiency advanced methods of supervision, fault-detection and fault diagnosis become increasingly important formany technical processes. This holds ...