• 9.类与对象二----重载、值传递、可变个数形参


    package com.atguigu.java;
    /*
     *一、理解万事万物皆对象
     *1.在Java语言范畴中,我们都将功能结构等都封装到类中,通过类的实例化,来调用具体的功能结构
     *2.涉及到JAVA语言与前端html、后端的数据库交互时,前后段的结构在Java层面交互时,都体现为类、对象
     *
     *
     *二、匿名对象的使用
     *1.创建的对象,没有显示的赋值给一个变量名
     *2.特征:匿名对象只能调用一次
     *
     */
    public class InstanceTest {
     public static void main(String[] args) {
      Phone p = new Phone();
      
      p.sendEmail();
      p.playGame();
      
      //匿名对象
      new Phone().price = 999;
      new Phone().showPrice();//0.0
      //匿名对象使用
      PhoneMall mall = new PhoneMall();
      mall.show(new Phone());
     }
    }
    class PhoneMall{
     public void show(Phone phone) {
      phone.sendEmail();
      phone.playGame();
     }
    }
    class Phone{
     double price;
     public void  sendEmail() {
      System.out.println("发送邮件");
     }
     
     public void playGame() {
      System.out.println("打游戏");
     }
     public void showPrice() {
      System.out.println(price);
     }
    }

    **********************************************************************************************************************************************

    package com.atguigu.java1;
    /*
     * 方法的重载(overload)
     *
     * 1.定义:在同一个类中,允许存在一个以上的同名方法,只要他们的参数个数或者参数类型不同即可
     *   
     *    两同一不同:同一个类、相同方法名
     *            参数列表不同;参数个数不同;参数类型不同
     *           
     * 2.举例
     *   Arrays类中的sort()/二分查找
     *
     * 3.判断是否重载
     *   跟方法的权限修饰符、返回值类型、形参变量名、方法体都没有关系
     * 
     * 4.通过对象调用方法时,如何确定某个指定方法:
     *     方法名-->参数列表
     */
    public class OverLoadTest {
     public static void main(String[] args) {
      OverLoadTest test = new OverLoadTest();
      test.getSum(1,2);
     }
             
     public void getSum(int i, int j) {
      System.out.println(i+j);
     }
     
     public void getSum(double d1,double d2) {
      
     }
     public void getSum(String s,int i) {
      
     }
     public void getSum(int i,String s) {
      
     }
     //public int getSum(int i,int j){
     
     //}
    }

    *******************************************************************************************************************************************

    package com.atguigu.java1;
    /*
     *关于变量的赋值
     * 如果变量是基本数据类型,此时赋值的是变量所保存的数据值。
     * 如果变量是应用数据类型 ,此时赋值的是所保存数据的地址值
     */
    public class ValueTransferTest {
     public static void main(String[] args) {
      System.out.println("************基本数据类型*******");
      int m = 10;
      int n = m;
      
      System.out.println("m= "+ m +",n=" + n);
      
      n=20;
      System.out.println("m= "+ m +",n=" + n);
      System.out.println("****************引用数据类型**********");
      Order o1 = new Order();
      o1.Orderid = 101;
      Order o2 = o1;
      System.out.println("o1.Order= "+ o1.Orderid +",o2.Order=" + o2.Orderid);
      o2.Orderid = 102;
      System.out.println("o1.Order= "+ o1.Orderid +",o2.Order=" + o2.Orderid);
     }
    }
    class Order{
     int Orderid;
    }
    ***********************************************************************************************************************************************
    package com.atguigu.java1;
    /*
     * 方法的形参的传递机制:值传递
     * 1.形参:方法定义时,声明的小括号内的参数
     *   实参:方法调用时,实际传递给形参的数据
     *
     *
     * 2.值传递机制描述
     * 如果参数是基本数据类型,此时实参赋值给形参的是实参真实存储的数据值。
     * 如果参数是引用数据类型,此时实参赋值给形参的是实参存储数据的地址值。
     */
    public class ValueTransferTest1 {
       public static void main(String[] args) {
       //
      int m = 10;
      int n = 20;
      System.out.println("m= "+ m +",n=" + n);
      //交换两个变量的值得操作
      //int temp = m;
      //m= n;
      //n=temp;
      
      ValueTransferTest1 test = new ValueTransferTest1();
      test.swap(m, n);
      System.out.println("m= "+ m +",n=" + n);
     }
     public void swap(int m,int n) {
      int temp = m;
      m=n;
      n=temp;
     }
    }
    *****************************************************************************************************************************************
    package com.atguigu.java1;
    public class ValueTransferTest2 {
     public static void main(String[] args) {
      Data data = new Data();
      data.m = 10;
      data.n = 20;
      System.out.println("m= "+ data.m +",n=" + data.n);
      
      ValueTransferTest2 test = new ValueTransferTest2();
      test.swap(data);
      System.out.println("m= "+ data.m +",n=" + data.n);
      
     }
     
     public void swap(Data data) {
      int temp = data.m;
      data.m = data.n;
      data.n = temp;
     }
        
    }
    class Data{
     int m;
     int n;
    }
    **********************************************************************************************************************************************
    package com.atguigu.java1;
    /*
     * 可变个数形参的方法
     *
     * 1.jdk5.0 新郑内容
     * 2.具体使用
     *   2.1 可变个数形参的格式:数据类型 ... 变量名
     *   2.2 当调入可变个数形参的方法时,传入参数个数可以是:0个、1个、2个……
     *   2.3 可变个数形参的方法与本类中方法名相同,形参不同的方法之间构成重载
     *   2.4 可变个数形参的方法与本类中方法名相同,形参类型也相同的数组之间不构成重载,二者不能共存
     *   2.5 可变个数形参在方法的形参中,必须声明在末尾,最多只能声明一个可变形参
     */
    public class MethodArgsTest {
     public static void main(String[] args) {
       MethodArgsTest test = new  MethodArgsTest();
       test.show(12);
    //   test.show("hello","world");
    //   test.show();
       test.show(new String[] {"AA","BB","CC"});
     }
     
     public void show(int i) {
      
     }
     
     public void show(String s) {
      System.out.println("show string");
      
     }
     
    // public void show(String ... strs) {
    //     for(int i=0;i<strs.length;i++) {
    //   System.out.println(strs[i]);
    //     }
    //  System.out.println("show String 。。。 strs");
    // }
     public void show(String[] strs) {
      
     }
     //2.5  The variable argument type String of the method
     //show must be the last parameter
     //public void show(String ...strs1,int i) {  
     //}
      public void show(int i,String ...strs1) {
      
      }
    }
    *********************************************************************************************************************************************
     
  • 相关阅读:
    java时间戳与Date相互转换、日期格式化、给日期加上指定时长、判断两时间点是否为同一天
    notepad++去掉红色波浪线
    发生异常Address already in use: bind
    SecureCRT背景颜色
    linux查看实时日志命令
    idel上传代码到github时遇到的Push rejected: Push to origin/master was rejected
    git解决error: The following untracked working tree files would be overwritten by checkout
    使用SecureCRT工具上传、下载文件的两种方法
    Windows下Zookeeper启动zkServer.cmd闪退问题的解决方案
    Maven的Snapshot版本与Release版本
  • 原文地址:https://www.cnblogs.com/sun1997/p/12513811.html
Copyright © 2020-2023  润新知