Exemples: heritage
SuperString.java
class SuperString {
public static void main(String [] args) {
Chat c = new Chat();
System.out.println(c);
System.out.println((Mammifere)c);
System.out.println(c.chatCouleur);
//System.out.println(((Mammifere)c).chatColor);
//Heritage.java:15: cannot find symbol
// symbol : variable chatColor
// lochation: class Mammifere
}
}
class Animal {
int poids=56;
public String toString() {
return "Je suis un animal (" + poids + "kg) ";
}
}
class Mammifere extends Animal {
int poids=100;
public String toString() {
return "Je suis un mammifere (" + poids + "kg) " + super.toString();
}
}
class Chien extends Mammifere {
int poids=20;
public String toString() {
return "Je suis un chien (" + poids + "kg) " + super.toString();
}
}
class Chat extends Mammifere {
int poids=5;
String chatCouleur="marron";
public String toString() {
return "Je suis un chat " + chatCouleur + " (" + poids + "kg) " + super.toString();
}
}
Shapes.java
import java.util.ArrayList;
abstract class Shape {
protected double x;
protected double y;
Shape() {}
Shape(double x, double y) {
this.x=x;
this.y=y;
}
void move (double x, double y) {
this.x+=x;
this.y+=y;
}
public String toString() {
return "x=" + x + ", y=" + y;
}
double getX() {
return x;
}
double getY() {
return y;
}
abstract double area();
abstract double perimeter();
abstract void scale(double s);
}
class Point extends Shape {
Point(double x,double y) {
super(x,y);
}
double area() {
return 0;
}
double perimeter() {
return 0;
}
void scale(double s) {}
public String toString() {
return "Point " + super.toString();
}
}
class Square extends Shape {
double l;
Square(double x, double y, double l) {
super(x,y);
this.l=l;
}
double perimeter() {
return l*4;
}
double area() {
return l*l;
}
void scale(double s) {
l *= s;
}
public String toString() {
return "Square len=" + l + " area=" + area()
+ " peri=" + perimeter() + " " + super.toString();
}
}
class Circle extends Shape {
double r;
Circle(double x, double y, double r) {
super(x,y);
this.r=r;
}
double perimeter() {
return 2 * Math.PI * r;
}
double circumference() {
return perimeter();
}
double area() {
return Math.PI * r *r;
}
void scale(double s ) {
r *= s;
}
public String toString() {
return "Circle rad=" + r + " area=" + area()
+ " circ=" + circumference() + " " + super.toString();
}
}
class Shapes {
public static void main(String [] args) {
ArrayList<Shape> a = new ArrayList<Shape>();
a.add(new Circle(10,12,6));
a.add(new Point(55,4));
a.add(new Square(9,45,34));
System.out.println(a);
for (Shape sh : a) {
sh.scale(.5);
sh.move(-3,-3);
}
System.out.println("After scaling");
for (Shape sh : a) {
System.out.println(sh);
}
}
}
SmartGuys.java
import java.util.ArrayList;
class SmartGuysInc {
public static void main(String [] args) {
Staff personnel = new Staff();
personnel.payday();
}
}
class Employee extends StaffMember {
protected double payRate;
public Employee(String empName, double empRate) {
super(empName);
payRate = empRate;
}
public double pay() {
return payRate;
}
public String toString() {
return super.toString() + "\nPay Rate: " + payRate;
}
}
class Executive extends Employee {
private double bonus;
public Executive(String exName, double exRate) {
super(exName, exRate);
bonus = 0;
}
public void awardBonus(double amount) {
bonus = amount;
}
public double pay() {
double paycheck = super.pay() + bonus;
bonus = 0;
return paycheck;
}
public String toString() {
return super.toString() + "\nCurrent bonus: " + bonus;
}
}
class Hourly extends Employee{
private int hours;
public Hourly(String hName, double hRate) {
super(hName, hRate);
hours = 0;
}
public void addHours(int moreHours) {
hours += moreHours;
}
public double pay() {
return payRate * hours;
}
public String toString() {
return super.toString() + "\nCurrent Hours: " + hours;
}
}
class Staff {
ArrayList<StaffMember> staffList;
public Staff() {
staffList = new ArrayList<StaffMember>();
staffList.add(new Executive("Bill Gates", 50000));
staffList.add(new Employee("Steve Jobs", 10000));
staffList.add( new Hourly("Dennis Ritchie", 200));
staffList.add(new Executive("Marc Andreessen", 12000));
staffList.add(new Hourly("Linus Torvalds", 150));
staffList.add(new Employee("James Gosling", 4000));
((Executive)staffList.get(0)).awardBonus(11000);
((Executive)staffList.get(3)).awardBonus(11000);
for(StaffMember sm : staffList) {
if(sm instanceof Hourly) {
((Hourly)sm).addHours(80);
}
}
}
public void payday() {
for (StaffMember sm : staffList) {
System.out.println(sm);
System.out.println("Paid: " + sm.pay());
System.out.println("***************************");
}
}
}
abstract class StaffMember {
protected String name;
public StaffMember(String empName) {
name = empName;
}
abstract public double pay();
public String toString() {
return "Name: " + name;
}
}
Last modified: Tue Sep 27 14:22:56 EDT 2005
par Douglas Eck [douglas D0T eck AT umontreal D0T ca]