java继承的代码 java继承的用法
JAVA:编写一个动物的继承关系代码.
下面是一个简单的 Java 程序示例,用于实现山羊和狼的继承关系,并在测试类中进行验证:
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、网络空间、营销软件、网站建设、古交网站维护、网站推广。
Animal.java:
```java
public abstract class Animal {
public void walk() {
System.out.println("走路");
}
public abstract void eat();
}
```
Goat.java:
```java
public class Goat extends Animal {
@Override
public void eat() {
System.out.println("吃草");
}
}
```
Wolf.java:
```java
public class Wolf extends Animal {
@Override
public void eat() {
System.out.println("吃肉");
}
}
```
AnimalTest.java:
```java
public class AnimalTest {
public static void main(String[] args) {
Goat goat = new Goat();
wolf wolf = new wolf();
goat.eat(); // 输出:吃草
wolf.eat(); // 输出:吃肉
goat.walk(); // 输出:走路
wolf.walk(); // 输出:走路
}
}
```
在此程序中,我们定义了一个 `Animal` 类作为基类,包含了所有动物都会有的行为(例如走路),并使用抽象方法 `eat()` 表示不同动物腊誉的饮食习惯。然后通过继承实现 `Goat` 和 `Wolf` 两个子类,并分别重写判缓 `eat()` 方法。
在测试类中,我们分别创建了一个 `Goat` 和一个 `Wolf` 对象,并分别调用其 `eat()` 和 `walk()` 方法进行验证。可以看到,不同的动物具备不同的饮食习惯,但轮冲段它们的走路行为是一致的。
java编写动物世界的继承关系代码
我写了一个,内容比较简单的。代码如下:public class AnimalTest {
Animal animal;
public void eat(Animal animal){
animal.eat();
}
public void walk(Animal animal){
animal.walk();
}
public static void main(String args[]){
Animal animal=new Animal("animal");
Wolf w=new Wolf("wolf");
Goat g=new Goat("羡棚goat");
AnimalTest at=new AnimalTest();
at.eat(animal);
at.eat(w);
at.eat(g);
at.walk(animal);
at.walk(w);
at.walk(g);
}
}
class Animal {
String name;
public Animal(String name){
this.name=name;
}
public Animal(){}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void eat(){
System.out.println("animal eat");
}
public void walk(){
System.out.println("animal walk");
}
public String toString(){
return name;
}
}class Wolf extends Animal {
public Wolf(String name){
super(name);
}
public Wolf(){}
public void eat(){
System.out.println("wolf eat meat");
}
public void walk(){
System.out.println("wolf walk");
}
public String toString(){
return name;
}
}class Goat extends Animal {
public Goat(String name){
super(name);
}
public Goat(){}
public void eat(){
System.out.println("改派坦核桐goat eat grass");
}
public void walk(){
System.out.println("goat walk");
}
public String toString(){
return name;
}
}
java 类的继承,求大神给代码
public class JIhe {
private String color;
private String dateCreated;
private String filled;
public String getColor() {
return color;
}
public String getDateCreated() {
return dateCreated;
}
public String getFilled() {
return filled;
}
public void setColor(String color) {
this.color = color;
}
public void setFilled(String filled) {
this.filled = filled;
}
@Override
public String toString() {
return "Color:" + this.color +" filled:" + this.filled + "detaCreated:" + dateCreated;
}
}
------------------------------------------------------------------------------------------------------------
public class Circle extends JIhe {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return 3.14 * this.radius * this.radius;
}
public double getPerimeter() {
return 2 * 3.14 * this.radius;
}
public double getDiameter() {
return 2 * this.radius;
}
}
-----------------------------------------------------------------------------------------------------
public class Rectangle extends JIhe {
private double width;
private double height;
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea(){
return this.width * this.height;
}
public double getPerimeter(){
return this.width * 2 + this.height * 2;
}
}
——庆腔简——————誉裤———圆知———————————————————————————
public class Test {
public static void main(String[] args){
Circle circle = new Circle();
circle.setRadius(1.0);
System.out.println(circle.getArea());
System.out.println(circle.getColor());
System.out.println(circle.getDateCreated());
System.out.println(circle.getDiameter());
System.out.println(circle.getFilled());
System.out.println(circle.getPerimeter());
System.out.println(circle.getRadius());
Rectangle r = new Rectangle();
r.setHeight(2.0);
r.setWidth(4.0);
System.out.println(r.getArea());
System.out.println(r.getColor());
System.out.println(r.getDateCreated());
System.out.println(r.getFilled());
System.out.println(r.getHeight());
System.out.println(r.getPerimeter());
System.out.println(r.getWidth());
}
}
JAVA 继承的代码 求帮助 在线等
简单写了下,希望空型对你有帮助
建议你找一下Java转型方面的资料
class Vehicle{
protected void vehicleRun(){
System.out.println("vehicle!!!");
}
}
class Truck extends Vehicle{
//子类覆写父类“vehicleRun方法”
protected void vehicleRun(){
System.out.println("It's Truck!!!");
}
protected void truckRun(){
System.out.println("源基truck!!!");
}
}
class Test{
public static void main(String [] args){
Vehicle v = new Vehicle();
v.vehicleRun(); //父类方法
//向上转型
Vehicle v1 = new Truck();
v1.vehicleRun(); //子类覆写后方法
//雹亏谨向下转型
Truck t = (Truck)v1;
t.vehicleRun();
t.truckRun();
}
}
分享文章:java继承的代码 java继承的用法
网址分享:http://myzitong.com/article/ddpgsch.html