Java子类继承父类方法:示例讲解
{"title":"Java子类继承父类方法:示例讲解","description":"本文通过一个矩形和立柱的示例,讲解了如何在Java子类中使用父类的方法。包括直接调用父类方法名和使用super关键字调用父类方法。","keywords":"Java, 子类, 父类, 继承, 方法调用, super, 示例, 矩形, 立柱","content":"public class Rectangle { \t\tint length; \t\tint wide; \t\t\tpublic Rectangle(int length, int wide) { \t\t\t\tthis.length = length; \t\t\t\tthis.wide = wide; \t\t\t} \t\t\tpublic Rectangle() {} \t\t\tpublic void perimeter() { \t\t\t\tSystem.out.println("周长是:"+(length+wide)2); \t\t\t} \t\t\tpublic void area() { \t\t\t\tSystem.out.println("面积是:" +lengthwide); \t\t\t} } public class Column extends Rectangle { \t\tint height; \t\tpublic Column(int length, int wide, int height) { \t\t\tsuper(length, wide); \t\t\tthis.height = height; \t\t} \t\t\tpublic void surfaceArea() { \t\t\t\tperimeter(); \t\t\t\tarea(); \t\t\t} } 怎么在子类中使用父类方法内容:在子类中使用父类的方法可以直接调用父类的方法名。在子类中可以使用super关键字来调用父类的方法。\n例如,在子类Column中使用父类Rectangle的perimeter方法和area方法可以这样写:\n\npublic void surfaceArea() {\n\tperimeter();\n\tarea();\n}\n\n"}
原文地址: https://www.cveoy.top/t/topic/pA2E 著作权归作者所有。请勿转载和采集!