![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 146
Rep Power: 2
![]() |
alternative to this code?
is there a way that i can get rid of having to make a class static that i declare an object of in the main method?
like i made this program to practice utilizing abstract classes by the way, it works PERFECTLY without errors WITH the bolded code. import java.util.Random;
public abstract class Ship {
private String name;
public Ship(String name2)
{name2=name;}
public String GetName()
{return name;}
public abstract int hit();
public abstract int shoot();
public static class Battleship extends Ship {
private int square; private int firepower;
public Battleship(int squares, int firepowers, String name)
{super(name); square=squares; firepower=firepowers;}
public int hit()
{
int i=square;
while (i>0)
{
shoot();
i--;
}
System.out.println("Hit "+a+" time(s) for "+g+" damage!!!");
return 0;
}
public int a; public int g;
public int shoot()
{
System.out.println("Salvo!");
Random r = new Random();
int v=r.nextInt(5)+1;
if (v<2)
{
System.out.println("HIT! For "+ firepower +" damage!");
a+=1;
g+=firepower;
}
return 0;
}
}
public static void main(String[] args) {
Battleship k = new Battleship(5,5,"q");
k.hit();
}
}non static variable cannot be referenced from a static context i heard something about making the class non public; would this do anything? i tried it and i get other errors... |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Ship is an abstract class, and as such should not contain any method definitions, including main.
Inner classes should (for the most part) only be used when the inner class will be used only by the outer class. Moreover, an inner class should be thought of as part of its outer class; a Battleship is a Ship, not part of one. Battleship objects should be be created outside the Ship object. You need to divide your code into three classes: Ship.java; Battleship.java; and Main.java (the naming of this last class is arbitrary), which might read: public class Main {
public static void main(String[] args) {
Ship k = new Battleship(5, 5, "q");
k.hit();
}
}Last edited by titaniumdecoy; Apr 20th, 2007 at 11:44 PM. |
|
|
|
|
|
#3 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
If Java abstract classes are anything like those in C#, having method definitions and constructors is both valid and useful. Even if it's abstract, a base class will probably still have common functionality that can be implemented. Even though you can't instantiate it, an abstract class may have constructors that can be called by the sublcass. Correct me if this is one of those few differences.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#4 | |
|
Expert Programmer
|
Quote:
![]() |
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 146
Rep Power: 2
![]() |
yeah i understand what you said, no confusion
what daemon said is right and since i DID use teh constructor super(name) which uses the abstract classes constructor, it has a use. thank you so much though titanium i realize how easy this was lol im stupid... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| EXECryptor software protection | Jean5 | C++ | 35 | Oct 10th, 2006 7:10 PM |
| Little help | whoawhoayoyo | Assembly | 8 | Apr 18th, 2006 7:10 PM |
| How to post a question | nnxion | C++ | 10 | Jun 3rd, 2005 11:53 AM |
| How to post a question | nnxion | C++ | 0 | Jun 3rd, 2005 8:55 AM |
| How to post a question | nnxion | C | 0 | Jun 3rd, 2005 8:55 AM |