zoukankan      html  css  js  c++  java
  • 类与对象作业2

    //例题1

    class Myclass
    {
     public String Information="";
     public void myMethod(String argu)
     {
      System.out.println(argu);
     }
     private int value;
     public int getValue()
     {
      return value;
     }
     public void setValue(int value)
     {
      this.value=value;
     }
    }
    public class zuoye1 {

     public static void main(String[] args) {
      // TODO 自动生成的方法存根

      Myclass obj=new Myclass();
      obj.myMethod("hello");
      obj.setValue(100);
      System.out.println(obj.getValue());
      obj.Information="information";
      System.out.println(obj.Information);
     }

    }

    //例题2

    class mytest
    {
     public int value;
     public boolean equals(Object obj)
     {
      return ((mytest)obj).value==this.value;
     }
     mytest(int v)
     {
      value=v;
     }
    }
    public class zuoye2 {

     public static void main(String[] args) {
      // TODO 自动生成的方法存根'
      mytest obj1=new mytest(100);
      mytest obj2=new mytest(100);
      System.out.println(obj1==obj2);
      System.out.println(obj1.equals(obj2)); 
     }

    }

    //例题3

    class mytest1
    {
     public int value;
     public boolean equals(mytest1 obj)
     {
      return obj.value==this.value;
     }
     mytest1(int v)
     {
      value=v;
     }
    }
    public class zuoye3 {

     public static void main(String[] args) {
      // TODO 自动生成的方法存根'
      mytest1 obj1=new mytest1(100);
      mytest1 obj2=new mytest1(100);
      System.out.println(obj1==obj2);
      System.out.println(obj1.equals(obj2)); 
     }

    }

    //例题4

    class ini
    {
     {field=200;}
     public int field=100;
     public ini(int value)
     {
      this.field=value;
     }
     public ini()
     {
      
     }
    }


    public class zuoye4 {

     public static void main(String[] args) {
      // TODO 自动生成的方法存根
      ini obj=new ini();
      System.out.println(obj.field );
      obj=new ini(300);
      System.out.println(obj.field );

     }

    }

    在类中多次初始化时按第一次赋值来运算;

    //例题5  静态函数如何引用非静态成员变量

    class sta
    {
     static int a;
     int b;
     static void ceshi(sta z)
     {
      a=1;
      z.b=2;
      
     }
    }

    class sta
    {
     static int a;
     int b;
     static void ceshi()
     {

      sta z;
      a=1;
      z.b=2;
      
     }
    }

    静态并不是属于类的一部分。想要引用非静态成员需要将类初始化,或者以参数的形式来应用。

  • 相关阅读:
    如何在Web项目中给没有添加API核心组件添加APIController的帮助页HelpPage
    如何在MVC_WebAPI项目中的APIController帮助页面添加Web测试工具测试
    exception throw in progress runner thread_VS2015中SVN源代码无说明提交异常
    [转]AMBA、AHB、APB、ASB总线简介
    UML和模式应用4:初始阶段(6)--迭代方法中如何使用用例
    UML和模式应用4:初始阶段(5)--用例编写的准则
    startup_MK64F12.s文件解析
    [转] bss段、data段、text段
    [转]GDB-----2.watchpoint
    [转]GDB-----1.GDB概述
  • 原文地址:https://www.cnblogs.com/837634902why/p/7697909.html
Copyright © 2011-2022 走看看