zoukankan      html  css  js  c++  java
  • java 类的继承

    package testpacknm;
    
    import java.util.Scanner;
    import testpacknm.testcnm;
    
    class Another {
        String name;
    }
    
    public class testcnm extends Another {
        int salary;
    
        public static void main(String[] args){
            testcnm obj = new testcnm();
            obj.salary = 10000;
            obj.name = "Prakash";
            System.out.println("Name: " + obj.name);
            System.out.println("Salary: " + obj.salary);
        }
    
    }

     2

    package testpacknm;
    
    import java.util.Scanner;
    import testpacknm.testcnm;
    
    
    class Salary extends Details {
        public void SalaryMethod() {
            System.out.println("Calling the salary method");
        }
    }
    
    class Details {
        public void detailMethod() {
            System.out.println("Calling detail method");
        }
    }
    
    public class testcnm {
        public static void main(String[] args) {
            Salary sal = new Salary();
            sal.detailMethod();
            sal.SalaryMethod();
        }
    }

     3

    package testpacknm;
    
    import java.util.Scanner;
    import testpacknm.testcnm;
    
    
    public class testcnm {
        public static void main(String[] args) {
            Salary sal = new Salary();
            sal.detailMethod();
            sal.SalaryMethod();
            sal.data();
        }
    }
    
    class Salary extends Details {
        public void SalaryMethod() {
            System.out.println("Calling the salary method");
        }
    }
    
    class Details extends Database {
        public void detailMethod() {
            System.out.println("Calling detail method");
        }
    }
    
    class Database {
        public void data() {
            System.out.println("Callind database method");
        }
    }

     4

    /**
     * Written for 'JavaDump' Android Application
     */
    // Hierarchical Inheritance Example
    public class Employee {
        public static void main(String[] args) {
            Salary sal = new Salary();
            // sal.detailMethod(); //throws error
            sal.SalaryMethod();
            sal.data();
        }
    }
    
    class Salary extends Database {
        public void SalaryMethod() {
            System.out.println("Calling the salary method");
        }
    }
    
    class Details extends Database {
        public void detailMethod() {
            System.out.println("Calling detail method");
        }
    }
    
    class Database {
        public void data() {
            System.out.println("Callind database method");
        }
    }
    
    
    OUTPUT:
    Calling the salary method
    Callind database method
  • 相关阅读:
    cuda基础----流
    cuda基础---异步并行执行
    cuda基础---cuda通信机制
    cuda基础-----cuda编程模型/软件体系/存储器模型
    大顶堆 小顶堆应用----中位数查找
    Maven配置阿里云镜像
    C++中bool型变量按位取反总是为true
    区分C++的继承、覆盖、隐藏、重载
    C++Primer第5章 语句
    C++Primer第4章 表达式
  • 原文地址:https://www.cnblogs.com/sea-stream/p/12076029.html
Copyright © 2011-2022 走看看