方法和构造函数

发布于 2025-01-03 22:53:58 字数 1023 浏览 1 评论 0原文

如何使用内部有代码的方法?

public void initialiseVariables(){
             name=timestamp.substring(0, 20);

            // numofdep = timestamp.substring(35, 37);
             //noofovertime = timestamp.substring(38, 40);
     if(timestamp.charAt(20)=='C')
         city=true;
     if(timestamp.charAt(21)=='U')
         union=true;
identificationnumber=Integer.parseInt(timestamp.substring(22,26));//to get an integer from a string use the integer.parseint
             numofdep = Integer.parseInt(timestamp.substring(35, 37));
             noofovertime = Integer.parseInt(timestamp.substring(38, 40));
     hoursworked=Integer.parseInt(timestamp.substring(27,29));
              hourlyrate = Double.parseDouble( timestamp.substring(30, 34));
 }

我不明白你在构造函数中放入了什么。另外,如何确定参数中放入的内容?就像我朋友写的这个构造函数。

 Employees(){

     this.timestamp=timestamp;
     initialiseVariables();
 }

那么我该如何

 name=timestamp.substring(0, 20);

在我的主类中使用这条线呢?

How do you use a method with code inside in?

public void initialiseVariables(){
             name=timestamp.substring(0, 20);

            // numofdep = timestamp.substring(35, 37);
             //noofovertime = timestamp.substring(38, 40);
     if(timestamp.charAt(20)=='C')
         city=true;
     if(timestamp.charAt(21)=='U')
         union=true;
identificationnumber=Integer.parseInt(timestamp.substring(22,26));//to get an integer from a string use the integer.parseint
             numofdep = Integer.parseInt(timestamp.substring(35, 37));
             noofovertime = Integer.parseInt(timestamp.substring(38, 40));
     hoursworked=Integer.parseInt(timestamp.substring(27,29));
              hourlyrate = Double.parseDouble( timestamp.substring(30, 34));
 }

I don't understand what you put inside the constructor. Also how do you determine what you put inside the parameters? like this constructor my friend wrote.

 Employees(){

     this.timestamp=timestamp;
     initialiseVariables();
 }

So how do I use for example the line,

 name=timestamp.substring(0, 20);

in my main class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时间你老了 2025-01-10 22:53:58

推荐阅读

班级员工:

 private String timestamp;

 Employees(String timestamp){
     this.timestamp=timestamp;
     initialiseVariables();
 }


private void initialiseVariables(){
    String name=timestamp.substring(0, 20);
    ...
}

public void doSomethingWithTimestamp(){...}

班级主要:

public static void main(String... args){
    Employees e = new Employees();
    e.doSomethingWithTimestamp();
}

Recommended reading

Class Employees:

 private String timestamp;

 Employees(String timestamp){
     this.timestamp=timestamp;
     initialiseVariables();
 }


private void initialiseVariables(){
    String name=timestamp.substring(0, 20);
    ...
}

public void doSomethingWithTimestamp(){...}

Class Main:

public static void main(String... args){
    Employees e = new Employees();
    e.doSomethingWithTimestamp();
}
守望孤独 2025-01-10 22:53:58

您的构造函数是错误的,您写道:

Employees(){

     this.timestamp=timestamp;
     initialiseVariables();
 }

您必须将构造函数构造为以下形式:

Employees([selected your type] timestamp){

     this.timestamp=timestamp;
     initialiseVariables();
 }

此构造函数有一个参数并将其设置为自己的对象成员(时间戳)并调用初始成员。
你必须读java core,《Thinking In Java》之类的书对你有帮助。

请充分利用您的方式以获得更好的帮助。

your constructor is wrong, you write :

Employees(){

     this.timestamp=timestamp;
     initialiseVariables();
 }

you have to constructor to following form :

Employees([selected your type] timestamp){

     this.timestamp=timestamp;
     initialiseVariables();
 }

this constructor has one parameter and set that to own object member(timestamp) and call initial member.
You have to read java core, books such as "Thinking In Java" help you.

please your means completely for better help.

睫毛上残留的泪 2025-01-10 22:53:58

看起来你在能走到这里之前就想先跑。我强烈建议您观看 theNewBoston 在 YouTube 上发布的以下教程视频。

http://www.youtube.com/watch?NR=1& ;v=SHIT5VkNrCg&feature=fvwp

只需花一些时间观看这些视频就会让您的事情变得更加轻松。我理解当你的老师把所有事情都扔给你而没有太多帮助时是什么感觉。这些视频会有帮助。

有时他会犯一些错误,但他的视频是我发现的最好的入门视频。祝你好运 :)

It's looking like you're trying to run before you can walk here. I'd strongly suggest taking a look at the following tutorial vidoes on youtube by theNewBoston.

http://www.youtube.com/watch?NR=1&v=SHIT5VkNrCg&feature=fvwp

Just spending some time watching these videos will make things MUCH easier for you. I understand what it's like when you have a teacher that throws everything at you without much help. Those videos will help.

Sometimes he gets things a little wrong, but his videos are the best I've found for getting started. Good luck :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文