给构造函数添加了新的方法,在实例中为什么不能调用

给构造函数添加了新的方法,在实例中为什么不能调用,第1张

在普通的方法中是不能调用构造方法的,但是在构造方法中可以调用其他的构造方法。

public class Test {

public Test(){

this("test")

//调用 Test(String str){}

//this()调用构造方法,通过参数来区分调用的是哪个构造方法。

//需要注意的就是,不可能出现递归调用的现象。

}

public Test(String str){

System.out.println(str)

}

}

在普通的方法中也没有调用构造方法的必要,如果是想调用构造方法中所写的代码,可以用如下方式。

public class Test {

public Test(){

this("test")

}

public Test(String str){

method1(str)

}

public void method1(String str){

System.out.println(str)

}

}

把构造方法中的代码写入一个方法中, 这样如果在想调用构造方法中的代码的话,直接调用method1就可以了。

例如要button1一打开就选中,可以用button1.Focus()就可以实现选中的效果了,添加的方法有两个:

1,在构造函数里添加,

public

Form1()

{

InitializeComponent()

button1.Focus()

}

2,双击窗口自动添加load函数,如:

private

void

Form1_Load(object

sender,

EventArgs

e)

{

button1.Focus()

}

#include <stdio.h>

void getstr(char* p){

printf("input:\t")

scanf("%s", p)

return

}

void main(){

char st[255]/*在程序中没有起多大作用*/

int top = 0

char str[255]

char k

int i=0

int st_error=0

getstr(str)

while ( (k=str[i]) != 0)

{

if (k == '(' ) st[top++] = k/*top++也一样,因为st这个栈空间没有起到存储的作用*/

if (k == ')' )

{

if (top == 0 )

{

st_error=1

break

}

else

top--

}

i++

}

if(st_error==0&&top==0) printf("匹配检查通过\n")

else

if(st_error==1) printf("缺少左括号!\n")

else

if(top>0) printf("缺少右括号!\n")

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11879843.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存