About Class on JAVA Programing
The following
Animal
class is one possible implementation of a animal:
class Animal
{
String food = "";
int leg = 0;
String respirator = "";
void addFood(String newFood) {
food = newFood;
}
void addLeg(int newLeg) {
leg = newLeg;
}
void addRespirator(String newRespirator) {
respirator = newRespirator;
}
void printOut(){
System.out.println("Food : "+food+" Leg : "+leg+" respirator : " + respirator);
}
}
to execute the class, we have to make the main class. We may have noticed that the Animal class does not contain a main
method.
That's because it's not a complete application; it's just the blueprint for animals that might be used in an application.
The responsibility of creating and using new Animal
objects belongs to some other class in your application.class BicycleDemo {
public static void main(String[] args) {
// Create two different Bicycle objects
Animal animal1 = new Animal();
Animal animal2 = new Animal();
// Invoke methods on those objects
animal1.addFood("tree");
animal1.addLeg(2);
animal1.addRespirator("gill");
animal1.printOut();
animal1.addFood("water");
animal1.addLeg(4);
animal1.addRespirator("lungs");
animal1.addFood("reptil");
animal1.printOut();
} }
The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:
Food : tree Leg : 2 respirator : gill
Food : reptil Leg : 4 respirator : lungs
0 comments
Post a Comment