Class 6 2ciclo

2
Class 6 Exposure: Group #7 Return of an object The keyword new Constructor call Return of an object The keyword new The new keyword is a Java operator that creates the object . The new operator precedes a call to a constructor , which initializes the new object . Constructor call A constructor can only be called by other constructors or class methods (static). public class BankAccount{ // member attributes or variables private double balance; public static int TotalAccounts =0; // methods public BankAccount ( ) { this(0.0); // constructor call has a parameter } public BankAccount( double ingreso ) { balance = write; incAccount (); } public double balance () { return balance; } public static void incCuentas () { TotalAccounts ++; } public void transfer (BankAccount origen ) { balance += origin. balance; origin. balance =0;

Transcript of Class 6 2ciclo

Page 1: Class 6 2ciclo

Class 6 Exposure: Group #7

Return of an object

The keyword new Constructor call

Return of an object The keyword new

The new keyword is a Java operator that creates the object . The new operator precedes a call to a constructor , which initializes the new

object .

Constructor call

A constructor can only be called by other constructors or class methods (static).

public class BankAccount{

// member attributes or variables

private double balance;

public static int TotalAccounts =0;

// methods

public BankAccount ( ) {

this(0.0); // constructor call has a parameter

}

public BankAccount( double ingreso ) {

balance = write;

incAccount ();

}

public double balance () {

return balance;

}

public static void incCuentas () {

TotalAccounts ++;

}

public void transfer (BankAccount origen ) {

balance += origin. balance;

origin. balance =0;

Page 2: Class 6 2ciclo

public class suma2 { public int a; public int b; public int c; public suma2(int a, int b) { this.a=a; this.b=b; } public void setCalculo() { c=(a+b)*-1; } public void getMostrar() { System.out.println("la suma es:"+c); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub suma2 e=new suma2(4,5); e.setCalculo(); e.getMostrar(); } }