SlideServe
Menu
Préférences
© SlideServe · v2026

Class

nita avatar
9 diapositives · 366 Views · Oct 13, 2014

Class. Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default one for you. Order of Class Composition: * Import statement if needed p ublic class ClassName

Class - slide 1
01 / 9Class
1 / 17

Télécharger la présentation

Class
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default one for you. Order of Class Composition: * Import statement if needed • public class ClassName • private Field data (instance variables) • public Constructor (method that creates the object) • public Methods ( get and set the instance variables and other actions as necessary)

  2. Instance Variables Properties of the object. What it knows about itself. Created in a field so also called field data private String color; private int legs; private String name; I know my color, how many legs I have and my name.

  3. CONSTRUCTOR public className() { Assign instance variables here } public Bug() { String color = “red”; int legs = 6; String name = “Beetle”; }

  4. Parameter ( ) (football) Supplies extra information to a method that it needs to execute. If it doesn’t need any it is blank. Required for constructors & all methods. public Bug() { // constructor public static void main(String[]args) { // method public void setName(“String name”); // method

  5. Classes create objects from the constructor with the word new public Bug() { String color = “red”; int legs = 6; String name = “Beetle”; } Default Constructor Bug b = new Bug(); Constructor that requires parameter info. when created. public Bug(String c, int l, String n) { color = c; legs = l; name = n; } Bug b = new Bug(“Red”, 6, “Beetle”); (String, int, String)

  6. Methods Accessor Methods: Access information from our instance variables. Instance variable: name name is a String public String getname() { return name; } Return methods must specify what data type they are returning in the method header.

  7. Mutator Methods Change information in our instance variables. Called setters. Instance Variable name public void setName(String n) { name = n; } Doesn’t return anything so it is void. Method that will set the instance variable for name to what is passed through the parameter when the method is called.

  8. Objects call their method with dot operator. Bug b = new Bug(); // b is the object public void setName(String n); // is the method So when you call the method you do the following: b.setName(“Harry”); // object.method System.out.println(b.getname()); //prints Harry

  9. Scanner Class import java.util.Scanner; // util package Constructor: public Scanner(Input Source) So to create a Scanner you do the following: Scanner sc = new Scanner(System.in); Scanner methods: sc.nextInt() stores ints sc.nextDouble() stores doubles sc.nextLine() stores next line as string sc.next() stores next as string