请问如果我朋友QQ不上线是不是不会产生距离产生美议论文

Java构造器,构造器重载
构造器一些基础语法见前面的笔记
l& 构造器:
& 当创建一个对象的时候会初始化对象属性,如果没有给对象属性赋值那么它就是默认值,构造器的作用就是初始化(就是赋值)这个对象属性的值,这就是的构造器作用。
& 构造器主要用于被其他方法调用,用以返回该类的实例,因而通常把构造器设置成public访问权限,从而允许系统中任何位置的类来创建该类的对象。除非不想让其他类创建该类的对象就不要用public。
&如果你没有提供构造器,系统默认带有一个不带参数的构造器,但是当你显示的创建了一个构造器后系统则不提供默认的构造器了,你可以自己在显示的添加一个不带参数的构造器。
public class ConstructorTest
&&&&&&&& publicS
&&&&&&&& publicConstructorTest(String name, int count)& //构造器
&&&&&&&& {
&&&&&&&&&&&&&&&&&& this.name=
&&&&&&&&&&&&&&&&&& this.count=
&&&&&&&& }
&&&&&&&& publicstatic void main(String[] args)
&&&&&&&& {
&&&&&&&&&&&&&&&&&& ConstructorTestcon = new ConstructorTest(&Fengqiang&,111);&&&&&&&&&&&&&&&&& //第一次给构造器传值,name=Fengqiang, count=111
&&&&&&&&&&&&&&&&&& System.out.println(con.name);
&&&&&&&&&&&&&&&&&& System.out.println(con.count);
&&&&&&&&&&&&&&&&&& ConstructorTestcon1 = new ConstructorTest(&Nina&,222);&&&&&&&&&&&&&&&&& //第二次给构造器传值,name=&Nina&,count=222 ,现在就是利用构造器方便给对象属性初始化了,这就是构造器的作用。
&&&&&&&&&&&&&&&&&& System.out.println(con1.name);
&&&&&&&&&&&&&&&&&& System.out.println(con1.count);
&&&&&&&& }
l& 构造器重载
& 构造器名字相同都跟类名一样,参数(参数个数或类型)不相同我们就说这是构造器重载。当你要多次初始化不同数量的对象属性时候可以使用构造器重载,因为这样给对象属性赋值方便些。
public class ConstructorTest
&&&&&&&& publicS
&&&&&&&& publicConstructorTest(String name)
&&&&&&&& {
&&&&&&&&&&&&&&&&&& this.name=
&&&&&&&&&&&&&&&&&& this.count=
&&&&&&&& }
&&&&&&&& publicConstructorTest(String name, int count)
&&&&&&&& {
&&&&&&&&&&&&&&&&&& this.name=
&&&&&&&&&&&&&&&&&& this.count=
&&&&&&&& }
&&&&&&&& publicstatic void main(String[] args)
&&&&&&&& {
&&&&&&&&&&&&&&&&&& ConstructorTestcon = new ConstructorTest(&Nina&);
&&&&&&&&&&&&&&&&&& System.out.println(con.name);
&&&&&&&&&&&&&&&&&& System.out.println(con.count);
&&&&&&&&&&&&&&&&&& ConstructorTestcon1 = new ConstructorTest(&Fengqiang&,333);
&&&&&&&&&&&&&&&&&& System.out.println(con1.name);
&&&&&&&&&&&&&&&&&& System.out.println(con1.count);
&&&&&&&& }
&构造器B代码,包含构造器A的所有代码,用this调用相应的构造器的初始化代码。
语法: this(参数列表)
&& 使用this调用另一个重载的构造器只能在构造器中使用,且必须作为构造器执行体的第一条语句。使用this调用重载的构造器时,系统根据this括号中的实际参数调用与之对应的形式参数列表的重载构造器。首先这样可以少些代码,其次当一个构造器中的代码修改后,不需要修改其他重载构造器中的代码。
软件程序规则:不要把相同的代码书写两次以上。
public class ConstructorTest
&&&&&&&& publicS
&&&&&&&& publicConstructorTest(String name)
&&&&&&&& {
&&&&&&&&&&&&&&&&&& this.name=
&&&&&&&& }
&&&&&&&& publicConstructorTest(String name, int count)
&&&&&&&& {
&&&&&&&&&&&&&&&&&& this(name);&&&&&&&&&&&&&& //调用形式参数列表为name的重载构造器初始化代码。
&&&&&&&&&&&&&&&&&& this.count=
&&&&&&&& }
&&&&&&&& publicConstructorTest(String name, int count, double weight)
&&&&&&&& {
&&&&&&&&&&&&&&&&&& this(name,count);&&&&&&&&&& //调用形式参数列表为name,count的重载构造器初始化代码。
&&&&&&&&&&&&&&&&&& this.weight=
&&&&&&&& }
&&&&&&&& publicstatic void main(String[] args)
&&&&&&&& {
&&&&&&&&&&&&&&&&&& ConstructorTestcon = new ConstructorTest(&Nina&);
&&&&&&&&&&&&&&&&&& System.out.println(con.name);
&&&&&&&&&&&&&&&&& System.out.println(con.count);
&&&&&&&&&&&&&&&&&& System.out.println(con.weight);
&&&&&&&&&&&&&&&&&& System.out.println(&&);
&&&&&&&&&&&&&&&&&& ConstructorTestcon1 = new ConstructorTest(&Fengqiang&,333);
&&&&&&&&&&&&&&&&&& System.out.println(con1.name);
&&&&&&&&&&&&&&&&&& System.out.println(con1.count);
&&&&&&&&&&&&&&&&&& System.out.println(con1.weight);
&&&&&&&&&&&&&&&&&& System.out.println(&&);
&&&&&&&&&&&&&&&&&& ConstructorTestcon2 = new ConstructorTest(&okay&,333,1.2345);
&&&&&&&&&&&&&&&&&& System.out.println(con2.name);
&&&&&&&&&&&&&&&&&& System.out.println(con2.count);
&&&&&&&&&&&&&&&&&& System.out.println(con2.weight);
&&&&&&&& }君,已阅读到文档的结尾了呢~~
JAVA语言中构造方法的继承与重载探讨JAVA
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
JAVA语言中构造方法的继承与重载探讨
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口Java中构造方法、类方法、final方法的重载与覆盖问题
Java中构造方法、类方法、final方法的重载与覆盖问题
重载: 方法名相同,但是参数必须有区别(参数不同可以使类型不同,顺序不同,个数不同)。
覆盖: 子类继承父类的方法,并重新实现该方法。
构造方法:和类同名。为新建对象开辟内存空间后,用于初始化新建的对象;类方法:静态方法,static修饰;使用类名作为前缀调用,而不是类的某个实例对象名;不能被单独对象拥有,属于整个类共享。final方法:final修饰,可被子类继承(即子类可以直接使用),但不能被子类覆盖;类中所有的private方法都隐式地指定为final。方法重载方法覆盖构造方法能被重载构造方法不能被覆盖(不能被继承)类方法能被重载类方法不能被重载final方法能被重载final方法不能被覆盖(子类不可见)
public class TestStaticMethod {
public static void main(String[] args) {
// TODO Auto-generated method stub
int ageMy = 12;
int sexMy = 2;
@SuppressWarnings(&unused&)
//CPeople.age = 0
System.out.println(CPeople.age);
//输出:24
CPeople.getMyAge(ageMy);
//ageMy属于类TestStaticMethod, ageMy=12
System.out.println(ageMy);
//CPeople.age = 0
System.out.println(CPeople.age);
//输出:14
CPeople.getMyAge(ageMy, sexMy);
//People.age = 0
System.out.println(People.age);
//输出:24
People.getMyAge(ageMy);
//People.age = 0
System.out.println(People.age);
//输出People
constructor ,如果没有对象people,
//不会输出People
constructor,因为静态对象不是通过构造方法初始化的
//二是通过静态初始化器进行的初始化。
People people = new People();
/*************************************
* 不能用对象显式的调用构造方法
people.People();
**************************************/
class People {
static int age,
//构造方法可以被重载,构造方法的定义没有返回值
public People() {
// TODO Auto-generated constructor stub
System.out.println(&People
constructor &);
public People(String string) {
// TODO Auto-generated constructor stub
System.out.println(&People
constructor: & + string);
//静态方法重载,参数个数不同
static int getMyAge(int age) {
age = age + 12;
System.out.println(age + &
People 1&);
static int getMyAge(int age, int sex) {
age = age +
System.out.println(age + &
People 2&);
class CPeople extends People{
/***********************************************
* 构造方法不能够覆盖,提示错误,可以通过super()的方式使用父类的构造器
* public People() {
// TODO Auto-generated constructor stub
System.out.println(&CPeople
constructor &);
************************************************/
public CPeople(String string) {
super(string);
// TODO Auto-generated constructor stub
//静态方法覆盖,子类覆盖了父类中两个年龄实现方法
static int getMyAge(int age) {
age = age + 12;
System.out.println(age + &
CPeople 1&);
static int getMyAge(int age, int sex) {
age = age +
System.out.println(age + &
CPeople 2&);
我的热门文章
即使是一小步也想与你分享Java方法的重载以及构造函数的理解_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Java方法的重载以及构造函数的理解
来源:Linux社区&
作者:weasleyqi
一直对重载和构造函数的概念不是很理解,看了mars的视频以后有一种豁然开朗的感觉,写下来跟大家一起分享下。
方法的重载有3个条件:
1、函数位于同一个类下面;
2、方法名必须一样;
3、方法的参数列表不一样。
比如有以下的例子:
class&Student&{&&
&&&&void&action(){&&
&&&&&&&&System.out.println("该函数没有参数!");&&
&&&&void&action(int&i)&&
&&&&&&&&System.out.println("有一个整形的参数!");&&
&&&&void&action(double&j)&&
&&&&&&&&System.out.println("有一个浮点型的参数!");&&
}&&该类中定义了3个方法,但是3个方法的参数列表不一样;
下面在主函数中调用这个类:
public&class&Test&{&&
&&&&public&static&void&main(String[]&args)&{&&
&&&&&&&&&&
&&&&&&&&&&&&Student&st&=&new&Student();&&
&&&&&&&&&&&&st.action();&&
&&&&&&&&&&&&st.action(1);&&
&&&&&&&&&&&&st.action(<FONT color=#c);&&
}&&看看运行结果:
从控制台的输出可以看出,我在主函数中实例化一个student对象,分别调用了这个对象的3中方法,由于3个方法的参数不一样,所以可以看到输出的结果也不一样;
构造函数的使用:
定义一个Sutdent类,类里面定义两个属性:
class&Student&{&&
&&&&String&&&
&&&&int&&&
}&&此时的Student类中没有构造函数,系统会自动添加一个无参的构造函数,这个构造函数的名称必须和类的名称完全一样,大小写也必须相同,系统编译完了之后是以下的情况:
class&Student&{&&
&&&&Student()&&
&&&&String&&&
&&&&int&&&
主函数中实例化两个对象:
public&class&Test&{&&
&&&&public&static&void&main(String[]&args)&{&&
&&&&&&&&&&
&&&&&&&&&&&&Student&st&=&new&Student();&&
&&&&&&&&&&&&st.name&=&"张三";&&
&&&&&&&&&&&&st.age&=&10;&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&Student&st2&=&new&Student();&&
&&&&&&&&&&&&st2.name&=&"李四";&&
&&&&&&&&&&&&st2.age&=&20;&&
}&&从主函数可以看出,此时的Student对象的属性比较少,创建的实例也比较少,如果属性多再创建多个实例的话,这个代码的量就很大,这时候,我们可以添加一个带参数的构造函数,如下:
class&Student&{&&
&&&&Student(String&n,&int&a)&&
&&&&&&&&name&=&n;&&
&&&&&&&&age&=&a;&&
&&&&String&&&
&&&&int&&&
}&&主函数的代码如下:
public&class&Test&{&&
&&&&public&static&void&main(String[]&args)&{&&
&&&&&&&&&&
&&&&&&&&&&&&Student&st&=&new&Student("张三",10);&&
&&&&&&&&&&&&Student&st2&=&new&Student("李四",20);&&
&&&&&&&&&&&&System.out.println("st的name:"&+&st.name&+",&st的age:"&+&st.age);&&
&&&&&&&&&&&&System.out.println("st2的name:"&+&st2.name&+",&st的age:"&+&st2.age);&&
}&&此时系统运行的结果如图:
从运行结果可以看出,我们在实例化Student对象的时候,调用了带参数的构造函数,节省了很多的代码,要注意:如果我们在Student类中定义了一个带参数的构造函数,但是没有写无参的构造函数,这个时候我们在主函数中就不能定义 Student st = new Student();如果在Student类中加上无参的构造函数就可以实现这样的实例化。
相关资讯 & & &
& (09/25/:24)
& (02/07/:50)
& (02/01/:30)
& (04/27/:16)
& (02/01/:53)
& (01/28/:24)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款Java使用super和this来重载构造方法 -
- ITeye技术网站
博客分类:
class anotherPerson{
String name = "";
String age = "";
public String getAge() {
public void setAge(String age) {
this.age =
public void setName(String name){
this.name =
public String getName(){
//第一个构造方法
public anotherPerson (String name){
this.name =
//第二个构造方法
public anotherPerson(String name, String age){
this(name);//是用同一类中的其他构造方法
this.age =
public void ShowInfomation(){
System.out.println("name is "+ name +"and age is "+age);
class Teacher extends anotherPerson{
String school = "";
public void setSchool(String school){
this.school =
public String getSchool(){
public Teacher(String name){
super(name);
//第一个构造方法
public Teacher(String age,String school){
super("babyDuncan",age);//使用父类的构造方法
this.school =
public Teacher(String name,String age,String school){
this(age,school);//使用同一类的构造方法,而这一构造方法使用父类的构造方法
this.name =
//重写了父类的函数
public void ShowInfomation(){
System.out.println("name is "+ name +" and age is "+age+" and school is "+school);
public class testTeacher {
* 测试一下super和this
public static void main(String[] args) {
// TODO Auto-generated method stub
anotherPerson person1 = new anotherPerson("babyDuncan");
anotherPerson person2 = new anotherPerson("babyDuncan","20");
Teacher teacher1 = new Teacher("babyDuncan");
Teacher teacher2 = new Teacher("20","JLU");
Teacher teacher3 = new Teacher("babyDuncan","20","JLU");
person1.ShowInfomation();
person2.ShowInfomation();
teacher1.ShowInfomation();
teacher2.ShowInfomation();
teacher3.ShowInfomation();
输出结果:
name is babyDuncanand age is
name is babyDuncanand age is 20
name is babyDuncan and age is& and school is
name is babyDuncan and age is 20 and school is JLU
name is babyDuncan and age is 20 and school is JLU
BabyDuncan
浏览: 348111 次
来自: 北京
好文章,顶顶
hugh.wang 写道请问你这个能还原吗?短网址用的是HAS ...
没有注释,看的费劲
好帖子竟然没人顶...
不好,你这只是记录式的博客,不是分享式的博客,对浏览着不友好. ...

我要回帖

更多关于 距离产生的不是美而是 的文章

 

随机推荐