将方法更改为静态方法并更改方法的调用(Java)(Change the method to a static method and change the invocation of the metho

编程入门 行业动态 更新时间:2024-10-27 01:27:30
方法更改为静态方法并更改方法的调用(Java)(Change the method to a static method and change the invocation of the method (Java))

快速注意:我还是新手编程,我以前的课程中没有涉及静态方法。 我曾尝试与我的教授取得联系,但似乎她现在有点忙于回应。 以下是未评分的作业; 我只是想知道我想在这里做什么。

我想要做的是:

创建一个名为HelloPrinter的类,该类具有一个将执行以下操作的方法:a。 接受一个int参数作为次数。 湾 使用该param作为循环中使用的sentinel值。 C。 在循环中,如果数字是奇数,则使用switch语句打印出“Hello,Zhang博士”,如果数字是偶数,则打印出“Hello,Class”。

在main方法中,实例化该类的对象并使用它来调用该方法。

将方法更改为静态方法并更改方法的调用

在HelloPrinter中,声明一个名为counter1的私有变量并将其初始化为零,声明另一个名为counter2的int变量并将其初始化为零 为counter2添加static一词。 为HelloPrinter类创建一个不接受参数的构造函数。 在构造函数的主体中,将counter1和counter2都加1。 在名为getCounter1()的类中定义另一个方法,该方法不接受任何参数但返回counter1的值。 对counter2做同样的事情 为HelloPrinter类定义toString()方法。 该方法将使用标签返回counter1和counter2的值。 测试主程序中的所有方法

我拥有的:

public class HelloPrinter{ private int counter1 = 0; private static int counter2 = 0; public HelloPrinter(int n){ for(int x=1;x<=n;x++){ int i=x; // System.out.println(x); <---testing only switch(i%2){ case 0: System.out.println("Hello Dr. Z"); break; case 1: System.out.println("Hello Class"); break; } }

}

(我也已经有getter(getCounter1 / getCounter2)和toString,只是不想泛滥。)我的问题是如何将main方法更改为静态方法并更改方法的调用? 需要做什么? 我该怎么做呢? 同样,这不是一个等级。 它应该是Java的复习。 不幸的是,上学期我们的班级落后了,因为她不断跳过部分,后来不得不回到他们身边(我去的大学需要不止一个编程教授。如果你问我)。

任何帮助都会很感激...谢谢 - 伊恩。

Quick note: I'm still new to programming and static methods were not covered in my previous class. I have tried getting into contact with my professor, but it seems that she is a tad too busy to respond at the moment. The following is a non-graded assignment; I just want to know what I'm suppose to be doing here.

What I'm suppose to be doing:

Create a class named HelloPrinter that has one method that will do the following: a. accept an int parameter as the number of times. b. use that param as the sentinel value to use in the loop. c. In the loop, use a switch statement to print out “Hello, Dr. Zhang” if the number is odd and “Hello, Class” if the number is even.

In the main method, instantiate an object of this class and use it to invoke the method.

Change the method to a static method and change the invocation of the method

In HelloPrinter, declare a private variable named counter1 and initialize it to zero, declare another int variable named counter2 and initialize it to zero Add the word static for counter2. Create a constructor for the HelloPrinter class that accepts no parameter. In the body of the constructor, increase both counter1 and counter2 by one. Define another method in the class named getCounter1() that accepts no parameters but returns the value of counter1. Do the same for counter2 Define a toString() method for the HelloPrinter class. The method will return the value of counter1 and counter2 with labels. Test all methods in the main program

What I have:

public class HelloPrinter{ private int counter1 = 0; private static int counter2 = 0; public HelloPrinter(int n){ for(int x=1;x<=n;x++){ int i=x; // System.out.println(x); <---testing only switch(i%2){ case 0: System.out.println("Hello Dr. Z"); break; case 1: System.out.println("Hello Class"); break; } }

}

(I also already have the getters (getCounter1/getCounter2) and the toString, just don't want to flood.) My question is how do I change the main method into a static method and change the invocation of the method? What needs to be done? And how would I go about doing it? Again, this is not for a grade. It is suppose to be a refresher on Java. Unfortunately last semester our class was behind because she kept skipping parts and having to go back to them later (the college I go to needs more than one programming prof. if you ask me).

Any help would be grateful... Thanks -Iain.

最满意答案

主要是静态的。

我认为你要做的是在你的班级中使用非静态方法进行打印。 要从main调用它,您需要从该类创建一个对象。 然后在您的类中添加静态方法。 要调用静态方法,您不需要从类中创建对象。

所以:

1-

public static void main(String[] args) { MyClass obj = new MyClass(); obj.nonStaticMethod(); }

2-

public static void main(String[] args) { MyClass.staticMethod(); }

此外,main可以在同一个类(MyClass)中,也可以只创建另一个包含main的类。

main is always Static.

I think what you are asked to do is to have a non-static method in your class that does the printing. In order to call it from main you need to create an object from that class. Then add a static method to your class. To call the static method, you won't need to create an object from your class.

So:

1-

public static void main(String[] args) { MyClass obj = new MyClass(); obj.nonStaticMethod(); }

2-

public static void main(String[] args) { MyClass.staticMethod(); }

Also, the main can be in the same class (MyClass) or you can create another class solely to contain main.

更多推荐

本文发布于:2023-07-31 00:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   静态   Java   Change   invocation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!