「java继承视频」用java编写一个继承程序
本篇文章给大家谈谈java继承视频,以及用java编写一个继承程序对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java的继承类如何设置
一个子类只能继承一个父类,一个父类可以被多个子类继承,所以你的问题是不可能实现的,如果你真像继承两个父类 就在新建个子类去继承那个父类,要用的时候只要调用这个子类的方法就可以了
java类的继承练习(一个小程序)
这就是多态的问题啦,子类继承父类,那么将子类对象的引用赋给父类对象,
你这里就是LX3_7_p sn=new LX3_8("aa",34,"bb","cc")将子类的引用赋给了父类,那么这个引用现在能调用到子类重写过的父类的方法以及父类的方法(这里我不知道该怎么说了,就是这样父类中有print()方法,子类中也就print()方法,并且返回类弄和参数都一样的话,就是重写了父类的方法,那么这时候用sn可以调用到print()方法,但这个方法不是父类的而是子类的),
而不能调用到子类中没有重写父类的方法(这里父类方法为PIRNT(),子类方法为PRIN()),所以sn根本调用不到子类的方法prin()不报错才怪
然后你将子类中的prin()改成print()这就相当于重写了父类的方法,在用sn调用时父类的方法被告覆盖,会调用到子类的print()方法.
不是引用出错啦,只是现在这个sn调用不到子类的prin()方法,你如果用LX3_8产生一个对象调用prin()是不会出现这种问题的啦.
用java继承编写三个类,要求如图
package school;
public class Test {
public static void main(String[] args) {
Student a = new Student1("小明", "我读的书是 java", "我是大一学生");
Student b = new Student1("小明", "我读的书是 马列主义", "我是大二学生");
Student c = new Student1("小明", "我读的书是 javaee", "我是大三学生");
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
/** 学生类 */
class Student {
private String name;
private String book;
private String type;
public Student(String name, String book, String type) {
this.name = name;
this.book = book;
this.type = type;
}
public String getName() {
return name;
}
public String getBook() {
return book;
}
public void setName(String name) {
this.name = name;
}
public void setBook(String book) {
this.book = book;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Student [name=" + name + ", book=" + book + ", type=" + type + "]";
}
}
/** 学生1 */
class Student1 extends Student {
public Student1(String name, String book, String type) {
super(name, book, type);
}
}
// /** 学生2 */
// class Student2 extends Student {
//
// public Student2(String name, String book) {
// super(name, book);
// }
//
// }
感觉没有类继承也能完成呢 不知道是不是这个意思 运行结果是
Student [name=小明, book=我读的书是 java, type=我是大一学生]
Student [name=小明, book=我读的书是 马列主义, type=我是大二学生]
Student [name=小明, book=我读的书是 javaee, type=我是大三学生]
java类的继承
2
class:Person;Name:lixiaolong;age:33
class:Student;Name:no name;age:0;majority:software technology
这是执行结果,第一个count为2是因为Person类的无参构造方法Person()被执行了两次,第二个结果不用解释new Person的时候调用它的有参构造方法给变量赋的值,第三个结果因为new Student的时候调用了Person的无参构造从始至终没有给变量赋过值。
我想你不明白的知识点应该是:在子类的构造方法中必须得调用父类的构造方法,如果父类有无参构造方法,则子类中可以省略super(),因为系统会默认调用了父类的无参构造方法,如果父类中只有有参构造方法,则子类中必须显示的调用,形式:super(参数)。希望你能看懂。
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编写一个继承程序、java继承视频的信息别忘了在本站进行查找喔。
发布于:2022-11-30,除非注明,否则均为
原创文章,转载请注明出处。