Contoh Java
inner_class.java
package inner_class; public class Inner_class { public static void main(String[] args) { System.out.println(" INNER CLASS"); System.out.println(); induk x = new induk(); System.out.println(" hasil penambahan ="+x.penambahan(20,5)); System.out.println(" hasil perkalian ="+x.kali(20, 5)); induk.anak y = x.new anak(); System.out.println("hasil perkalian =" +y.perkalian(10, 5)); induk.anak.cucu z = y.new cucu(); System.out.println("hasil pengurangan =" +z.pengurangan(25,5)); } }induk.java
package inner_class; public class induk { public int penambahan (int a,int b){ return a+b; } public int kali (int a,int b){ anak x = new induk.anak(); return x.perkalian(a,b); } public class anak { public int perkalian (int a,int b){ return a*b; } public class cucu{ public int pengurangan (int a,int b){ return a-b; } } } }Contoh C-Sharp
program.cs
using System; namespace contoh_inner_class_csharp { class Program { public static void Main(string[] args) { Console.WriteLine(" INNER CLASS"); Console.WriteLine(); Induk x = new Induk(); Console.WriteLine(" - Penambahan = "+ x.penambahan(5,5)); Console.WriteLine(" - Perkalian = "+ x.kali(10,5)); Induk.anak y = new Induk.anak(); Console.WriteLine(" - Perkalian = "+ y.perkalian(5,5)); Induk.anak.cucu z = new Induk.anak.cucu(); Console.WriteLine(" - Pengurangan = "+ z.pengurangan(10,5)); Console.ReadKey(true); } } }induk.cs
using System; namespace contoh_inner_class_csharp { public class Induk { public int penambahan (int a, int b) { return a+b; } public int kali (int a, int b){ anak x = new Induk.anak(); return x.perkalian(a,b); } public class anak { public int perkalian (int a, int b){ return a*b; } public class cucu { public int pengurangan (int a, int b){ return a-b; } }// end class cucu }// end class anak }// end class induk }semoga bermanfaat. . . .
0 komentar:
Posting Komentar