Académique Documents
Professionnel Documents
Culture Documents
uselocalvariable();
usefield();
uselocalvariable();
usefield();
///multiple constructors....
class sphere {
int radius = 0;
sphere() {
radius = 1;
}
sphere(int radius) {
this.radius = radius;
}
}
class sphere {
double radius; // radius of a sphere
sphere() {
// class constructor
sphere(double theradius) {
radius = theradius; // set the radius
amethod(sp);
}
depositamount = 10.10;
account1.credit( depositamount ); // add to account1 balance
depositamount = 12.12;
account2.credit( depositamount ); // add to account2 balance
class account
{
private double balance; // instance variable that stores the balance
// constructor
public account( double initialbalance )
{
if ( initialbalance > 0.0 )
balance = initialbalance;
}
5.4.2 dynamically changing the behavior of an object via composition (the 'state' design pattern)
class stage {
private actor actor = new happyactor();
happyactor
sadactor
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
class myclass {
int height;
myclass() {
system.out.println("planting a seedling");
height = 0;
}
myclass(int i) {
system.out.println("creating new tree that is " + i + " feet tall");
height = i;
}
void info() {
system.out.println("tree is " + height + " feet tall");
}
void info(string s) {
system.out.println(s + ": tree is " + height + " feet tall");
}
}
interface counter {
int next();
}
interface a {
string readlabel();
}
'import' statements must come after the package statement but before the class declaration.
the import keyword can appear multiple times in a class.
import java.io.file;
import java.util.list;
//...
}
interface a {
void amethod();
}
interface b {
void bmethod();
}
interface c {
void cmethod();
}
class classa {
public void amethod() {
}
}
the tostring method has an implementation, so you do not need to override this method.
the print method is declared abstract and does not have a body.
for example:
objectreference.methodname
objectreference.fieldname
the following code, for instance, creates an employee object and assigns values to its age and
salary fields:
class employee {
public int age;
public double salary;
public employee() {
}
public employee(int agevalue, double salaryvalue) {
age = agevalue;
salary = salaryvalue;
}
}
when an object is created, the jvm also performs initialization that assign default values to fields.
when you compare two reference variables a and b, such as in this code
if (a == b)
you are actually asking if a and b are referencing the same object, and not whether or not the
objects referenced by a and b are identical.
the type of object 'a' references is identical to the type of object 'b' references. however, a and b
reference two different instances, and a and b contains different memory addresses. therefore, (a
== b) returns false. to compare value for reference use
a.equals(b);
class point {
double x;
double y;
// you can use point objects in the definition of the class line:
class line {
point start;
point end;
double length() {
return start.distance(end);
}
system.out.println(l1);
}
a variable of type object can store a reference to an object of any class type.
class number {
int i;
}
1: n1.i: 9, n2.i: 47
2: n1.i: 47, n2.i: 47
3: n1.i: 27, n2.i: 27
class animal {
public animal(string atype) {
type = new string(atype);
}
public string tostring() {
return "this is a " + type;
}
private string type;
}
class dog extends animal {
public dog(string aname) {
super("dog");
name = aname;
breed = "unknown";
}
public dog(string aname, string abreed) {
super("dog");
name = aname;
breed = abreed;
}
public string tostring() {
return "it's " + name + " the " + breed;
}
private string name;
private string breed;
}
class animal {
public animal(string atype) {
type = new string(atype);
}
public string tostring() {
return "this is a " + type;
}
private string type;
}
class dog extends animal {
public dog(string aname) {
super("dog");
name = aname;
breed = "unknown";
}
public dog(string aname, string abreed) {
super("dog");
name = aname;
breed = abreed;
}
private string name;
private string breed;
}
5.19.9 type casting
with objects, you can cast an instance of a subclass to its parent class.
casting an object to a parent class is called upcasting.
to upcast a child object, all you need to do is assign the object to a reference variable of type
parent. the parent reference variable cannot access the members that are only available in child.
because parent references an object of type child, you can cast it back to child. it is called
downcasting because you are casting an object to a class down the inheritance hierarchy.
downcasting requires that you write the child type in brackets. for example:
class a {
a(int i) {
system.out.println("a constructor");
}
}
class b extends a {
b(int i) {
super(i);
system.out.println("b constructor");
}
}
class c extends b {
c() {
super(11);
system.out.println("c constructor");
}
}
a constructor
b constructor
c constructor
class characteristic {
private string s;
characteristic(string s) {
this.s = s;
system.out.println("creating characteristic " + s);
}
class description {
private string s;
description(string s) {
this.s = s;
system.out.println("creating description " + s);
}
class livingcreature {
private characteristic p = new characteristic("is alive");
livingcreature() {
system.out.println("livingcreature()");
}
animal() {
system.out.println("animal()");
}
amphibian() {
system.out.println("amphibian()");
}
public frog() {
system.out.println("frog()");
}