Vai al contenuto

Primario: Sky Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Secondario: Sky Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Sfondo: Blank Waves Squares Notes Sharp Wood Rockface Leather Honey Vertical Triangles
Corsi di Laurea










ROX @ Unisa - Forum degli studenti di Ingegneria utilizza i cookie. Se prosegui la navigazione accetti il loro uso.    Accetto l'uso dei cookie
-->
Foto

Problema con Java


  • Effettua l'accesso per rispondere
Questa discussione ha avuto 2 risposta/e

#1
Marika93

Marika93

    Advanced Member

  • Utente
  • StellaStellaStella
  • 358 Messaggi:
Salve ragazzi, mi sono inceppata con questo codice stupidissimo, mi dareste per cortesia una mano?
Il codice è questo
[codice-java:1ogucu91]package defaultconstructor;


public class Ex {
System.out.println("I'm a class");
}



package defaultconstructor;


public class DefaultConstructor {


public static void main(String[] args) {
Ex c = new Ex();

// TODO code application logic here
}
}[/codice-java]

e il problema è che mi segnala errore nella stampa nella classe, dice che non trova out in System... se copio la stampa nel main la fa, però la traccia (sul libro) dice esplicitamente che la stampa dev'essere fatta nella classe...


Quando non ci sono soluzioni significa che il problema non esiste.
Qual'è la differenza tra un meccanico, un tecnico ed un ingegnere? Il meccanico sostituisce il pezzo guasto, il tecnico ripara il guasto, l'ingegnere lo prevede!
La mente umana è come un paracadute, se non la apri non funziona!

#2
Marika93

Marika93

    Advanced Member

  • Utente
  • StellaStellaStella
  • 358 Messaggi:
Ho risolto modificando la classe in questo modo
[codice-java:2n9iwow9]package defaultconstructor;


public class Ex {

Ex(){
System.out.println("I'm a class");

}


}[/codice-java]
Quando non ci sono soluzioni significa che il problema non esiste.
Qual'è la differenza tra un meccanico, un tecnico ed un ingegnere? Il meccanico sostituisce il pezzo guasto, il tecnico ripara il guasto, l'ingegnere lo prevede!
La mente umana è come un paracadute, se non la apri non funziona!

#3
vi

vi

    Advanced Member

  • Utente
  • StellaStellaStella
  • 170 Messaggi:

You've seen classes defined in the following way:

[codice-java:2dpcvu56]class MyClass {
// field, constructor, and
// method declarations
}[/codice-java]

This is a class declaration. The class body (the area between the braces) contains all the code that provides for the life cycle of the objects created from the class: constructors for initializing new objects, declarations for the fields that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects.

The preceding class declaration is a minimal one. It contains only those components of a class declaration that are required. You can provide more information about the class, such as the name of its superclass, whether it implements any interfaces, and so on, at the start of the class declaration. For example,

[codice-java:2dpcvu56]class MyClass extends MySuperClass implements YourInterface {
// field, constructor, and
// method declarations
}[/codice-java]

means that MyClass is a subclass of MySuperClass and that it implements the YourInterface interface.

You can also add modifiers like public or private at the very beginning—so you can see that the opening line of a class declaration can become quite complicated. The modifiers public and private, which determine what other classes can access MyClass, are discussed later in this lesson. The lesson on interfaces and inheritance will explain how and why you would use the extends and implements keywords in a class declaration. For the moment you do not need to worry about these extra complications.

In general, class declarations can include these components, in order:

Modifiers such as public, private, and a number of others that you will encounter later.
The class name, with the initial letter capitalized by convention.
The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
The class body, surrounded by braces, {}.

http://docs.oracle.c.../classdecl.html


Riassumendo molto banalmente, il codice demandato all'elaborazione dev'essere incapsulato o all'interno di un costruttore (come nel secondo esempio da te pubblicato) o di un metodo.
P.S.: non dimenticare mai di dichiarare esplicitamente i modificatori di accesso per una corretta gestione del information-hiding.




Leggono questa discussione 0 utenti

0 utenti, 0 ospiti, 0 utenti anonimi