Java:
class circle { int r; float S() { return 3.14f * r * r; } } class ball extends circle { float V() { return 4 / 3f * 3.14f * r * r * r; } } public class c1 { public static void main(String[] args) { // TODO Auto-generated method stub ball b = new ball(); b.r = 2; System.out.println("球的截面积是:" + b.S() + "球的体积是:" + b.V()); } }
c#:
class circle { public int r; public float S() { return 3.14f * r * r; } } class ball:circle { public float V() { return 4 / 3f * 3.14f * r * r * r; } } class Program { static void Main(string[] args) { ball b = new ball(); b.r = 2; Console.WriteLine("球的截面积是:" + b.S() + "球的体积是:" + b.V()); Console.ReadKey(); } }
内存示意: