SlideServe
Menu
Préférences
© SlideServe · v2026

Constructors

roden avatar
5 diapositives · 238 Views · Aug 28, 2014

Constructors. Used to initialize the member variables Default constructor initializes these to zero Constructors are special type of methods Do not have a return type (not even void) Must be public so that they can be accessed from a different class

Constructors - slide 1
01 / 5Constructors
1 / 17

Télécharger la présentation

Constructors
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. Constructors • Used to initialize the member variables • Default constructor initializes these to zero • Constructors are special type of methods • Do not have a return type (not even void) • Must be public so that they can be accessed from a different class • Cannot be static: static methods can be invoked without attaching them to any object e.g., main; constructors by definition must be invoked for a particular object

  2. class example class Pwr { double b; int e; double val; Pwr(double base, int exp) { b = base; e = exp; val = 1; if(exp==0) return; for( ; exp>0; exp--) val = val * base; } double get_pwr() { return val; } } class power { public static void main(String args[]) { Pwr x = new Pwr(4.0, 2); Pwr y = new Pwr(2.5, 1); Pwr z = new Pwr(5.7, 0); System.out.println(x.b + " raised to the " + x.e + " power is " + x.get_pwr()); System.out.println(y.b + " raised to the " + y.e + " power is " + y.get_pwr()); System.out.println(z.b + " raised to the " + z.e + " power is " + z.get_pwr()); } }

  3. ‘this’ example class Pwr { double b; int e; double val; Pwr(double base, int exp) { this.b = base; this.e = exp; this.val = 1; if(exp==0) return; for( ; exp>0; exp--) this.val = this.val * base; } double get_pwr() { return this.val; } } class powerthis { public static void main(String args[]) { Pwr x = new Pwr(4.0, 2); Pwr y = new Pwr(2.5, 1); Pwr z = new Pwr(5.7, 0); System.out.println(x.b + " raised to the " + x.e + " power is " + x.get_pwr()); System.out.println(y.b + " raised to the " + y.e + " power is " + y.get_pwr()); System.out.println(z.b + " raised to the " + z.e + " power is " + z.get_pwr()); } }

  4. ‘this’ demo class Pwr { double b; int e; double val; Pwr(double b, int e) { this.b = b; this.e = e; val = 1; if(e==0) return; for( ; e>0; e--) val = val * b; } double get_pwr() { return this.val; } } class thisdemo { public static void main(String args[]) { Pwr x = new Pwr(4.0, 2); Pwr y = new Pwr(2.5, 1); Pwr z = new Pwr(5.7, 0); System.out.println(x.b + " raised to the " + x.e + " power is " + x.get_pwr()); System.out.println(y.b + " raised to the " + y.e + " power is " + y.get_pwr()); System.out.println(z.b + " raised to the " + z.e + " power is " + z.get_pwr()); } }

  5. Class demo class C{ int x=10, y=25; C(){ } C(int a, int b){ //System.out.println("printining from constructor"+x+" "+y); x=a; y=b; } void f(int x){ System.out.println(x+" "+this.x+" "+y); C d = new C(1,1); System.out.println(d.x+" "+this.x+" "+d.y); d = this; System.out.println(d.x+" "+this.x+" "+d.y); } } class classdemo{ public static void main(String args[]){ C a = new C(); a.f(5); C b = new C(15,20); b.f(10); } } 5 10 25 1 10 1 10 10 25 10 15 20 1 15 1 15 15 20