this
this
this
is a local variablethis
. Don't confuse them)BankAccount
againBankAccount
class definition to use
this
public BankAccount(String name)
public void deposit(int amount)
public void withdraw(int amount)
public int getBalance()
public int getNumTransactions()
public String toString()
public void transfer(int amount, BankAccount destination)
BankAccount
class definitionclass BankAccount { private String name; private int balance; private int numTransactions; public BankAccount(String name) { this.name = name; } public void deposit(int amount) { balance = balance + amount; numTransactions = numTransactions + 1; } public void withdraw(int amount) { balance = balance - amount; numTransactions = numTransactions + 1; } public int getBalance() { return balance; } public int getNumTransactions() { return numTransactions; } public String toString() { return "Account name: " + name + " Balance: " + balance + " Number of transcations: " + numTransactions; } public void transfer(int amount, BankAccount destination) { balance = balance - amount; numTransactions = numTransactions + 1; destination.balance = destination.balance + amount; destination.numTransactions = destination.numTransactions + 1; } }
BankAccountTester
class BankAccountTester { public static void main(String[] args) { BankAccount ac1 = new BankAccount("Sir Hugh Jeegoh"); BankAccount ac2 = new BankAccount("UCC"); ac2.deposit(1000); System.out.println(ac2.getBalance()); ac1.withdraw(50); System.out.println(ac1.getNumTransactions()); System.out.println(ac1); ac2.transfer(100, ac1); } }
self
in a similar way to how Java uses this
BankAccount
, in Java use of this
is not necessary
self
Python | Java |
---|---|
class Dog : def __init__(self, name, age, breed) : self.name = name self.age = age self.breed = breed def bark(self) : print("Ruff ruff!") def setName(self, name) : self.name = name def setAge(self, age) : self.age = age def setBreed(self, breed) : self.breed = breed def getName(self) : return self.name def getAge(self) : return self.age def getBreed(self) : return self.breed def __str__(self) : return self.name + " is a " + str(self.age) + " year old " + self.breed |
class Dog { private String name; private int age; private String breed; public Dog(String name, int age, String breed) { this.name = name; this.age = age; this.breed = breed; } public void bark() { System.out.println("Ruff ruff!"); } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setBreed(String breed) { this.breed = breed; } public String getName() { return name; } public int getAge() { return age; } public String getBreed() { return breed; } public String toString() { return name + " is a " + age + " year old " + breed; } } |
Python | Java |
---|---|
from dog import Dog d1 = Dog("Charles", 2, "Charlywawa") d2 = Dog("Colin", 7, "Colombian Drug Lord") d1.bark() d2.setAge(8) print(d2) |
class DogTester { public static void main(String[] args) { Dog d1 = new Dog("Charles", 2, "Charlywawa"); Dog d2 = new Dog("Colin", 7, "Colombian Drug Lord"); d1.bark(); d2.setAge(8); System.out.println(d2); } } |
private