构造函数和关键字 [英] Constructors and keywords

查看:96
本文介绍了构造函数和关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我11岁的儿子最近进入Java和Javascript编程,并问:



我想知道何时以及为何使用构造函数以及何时使用'this'和'super'关键字,例如



this.weight = weight ;



我们非常感谢任何帮助!

解决方案

构造函数声明与声明方法相同除了它们使用类的名称而没有返回类型。当你调用类构造函数时,它从类blueprint创建一个对象。你可以在同一个类中包含许多构造函数,包括没有参数构造函数。虽然你没有创建构造函数有一个与该类关联的默认构造函数,您可以调用它来创建一个对象。



关于这个关键字



  private   int 年龄; 
private String name;

public 学生( int 年龄, int 名称){
this .age =年龄;
.name =名称;
}




构造函数age中的
是构造函数的第一个参数的本地副本。要引用Point字段时代,构造函数必须使用this.age



关于 super 关键字



如果你的方法覆盖了它的一个超类的方法,你可以通过使用关键字super调用重写的方法。



参考文献:



为您的班级提供构造函数

使用关键字超级

使用此关键字


< blockquote>这些链接可以帮助你 -

Java中这个和超级关键字的区别 [ ^ ]

为您的类提供构造函数 [ ^ ]


重要的是,他了解Java和Javascript完全不同(即使它们看起来非常相似)。告诉他坚持一个或另一个,我的建议是忘记Javascript直到他开始创建网站,并坚持使用Java。学习所有语言的好地方是 Java教程 [ ^ ]。


Hi all,

My 11 year old son recently got into Java and Javascript programming, and asks:

"I want to know when and why to use constructors and when to use 'this' and 'super' keywords such as

this.weight = weight;"

Any help would be much appreciated!

解决方案

Constructor declaration is same as declaring a method except they use the name of the class and no return type.When you invoked a class constructor it creates a object from the class blueprint.You can have many constructors inside the same class including no argument constructor.Although you didn't create a constructor there is a default constructor associated with that class and you can invoke it to create an object out of it.

about this keyword

private int age;
private String name;

public Student(int Age, int Name) {
    this.age= Age;
    this.name = Name;
}



inside the constructor age is a local copy of the constructor's first argument. To refer to the Point field age, the constructor must use this.age

about super keyword

If your method overrides one of its super class's methods, you can invoke the overridden method through the use of the keyword super.

References :

Providing Constructors for Your Classes
Using the Keyword super
Using the this Keyword


These links could help you -
Difference between this and super keywords in Java [^]
Providing Constructors for Your Classes[^]


It's important that he learns that Java and Javascript are totally different (even though they look very similar). Tell him to stick to one or the other, my advice would be to forget Javascript until he starts creating websites, and stick to Java. A good place for learning all about the language is The Java tutorials[^].


这篇关于构造函数和关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆