Java语言入门(五)——面向对象(二)

类、对象、构造方法关系图

在这里插入图片描述
在这里插入图片描述

class Money
{  int i=5; 
   public Money() {  }
   public Money(int i) //构造方法, i为局部变量
   {  this.i=i;  }   //第一个i为局部变量,第二个i为成员变量
   public Money add() //成员方法,返回值是对象
   {  i++;     //i为成员变量
      return this;}
    void print()
   {System.out.println(i);}
} 
class Test-5.1
{  public static void main(String[] args)
   {  Money m1=new Money(10);
      m1.add().add().add().print();}     //13
}

垃圾回收机制

1 c++中的析构方法
2 java中的finalize()方法
3 System.gc的作用

class Rubbish
{
   public void finalize()
   {  System.out.println("rubbish is over!"); } 
    public static void main(String[] args)
    {
        new Rubbish();
        new Rubbish();
         //new Rubbish().finalize();
        System.gc();
     }
}

参数传递

1 基本数据类型的参数传递

class Test-5.2
{
	public static void main(String[] args)
	{
		int x=10;
		change(x);
		System.out.println(x);    //10
	}
	static void change(int x)
	{
		x+=15;
	}
}

在这里插入图片描述
2 引用数据类型的参数传递

class PP
{
      int x=0;	//成员变量在堆内存
      public static void main(String[] args)
       {
         int x=10;   //局部变量在栈内存
         change(new  PP());
         System.out.println(x);       //10
        //System.out.println(this.x);
       }
       static void change(PP y)
       {y.x=15;}
}

在这里插入图片描述
3 引用参数传递

class TT
{
    int x=0;  
    public static void main(String[] args)
    {
       int x=10;
       TT t=new TT();
        change(t);
        System.out.println(t.x);      //15
      }
      static void change(TT y) //引用传递
      {  y.x=15;  }  
} 

在这里插入图片描述

static关键字

1 静态变量,放在data seg,公共的属性,在内存中有且只有一份,又称为类变量,不用生成对象。
2 静态方法,也可以不用生成对象,用类名直接调用,静态方法只能调用静态方法或变量,非静态方法可访问静态方法或变量,这是因为,对于非静态的方法和变量,需要先创建类的实例对象后才可使用,而静态方法在使用前不用创建任何实例对象。

class Student
{  String name;
   static String school="GY";
   public Student(String name)
   { this.name=name; }
   void print() 
   {  System.out.println(this.name+":"+school); }
}
class Test-5.3
{  public static void main(String[] args)
   {  Student s1=new Student("huang");
      Student s2=new Student("wang");
      Student.school="GYD";  
      //也可用s1.school="GYD";一改全改
       s1.print();     //huang:GYD
       s2.print();}    //wang:GYD
}

在这里插入图片描述

class Student
{
   String name;
   static String school;
}
class Test-5.4
{  
    public static void main(String[] args)
    { 
       Student.school="GYD";
    }
}
class Student
{ String name;
   static String school;
   void ch1() //非静态的方法可以访问静态的变量
   {  System.out.println(school); }
   static void ch2() //静态方法可以访问静态的变量
   {System.out.println(school);
   //name="huang";错误,普通变量必须生成对象
}}
class Test-5.5
{  public static void main(String[] args)
   { Student.school="GYD";
     new Student().ch1();         //GYD
     new Student().ch2();         //GYD
   }
}
class HH
{  public static void main(String[] args)
   { int x=10;
     /*new HH().change(x);*/
     change(x);
     System.out.println(x);    //10
}
    static void change(int y) //引用传递,必须为static
    {  y=15;  }
     /*void change(int y)   {y=15;}
     不加static则需新建对象后再调用*/
}
class Test-5.6
{
   public static void main(String[] args)
   {
     int x=10;
     System.out.println(args[0]);      //LL
     System.out.println(args[1]);      //JJ SS
    }
}
//命令行参数,执行时为java Test-5.6  LL “JJ SS”

static的应用

1 统计对象
2 类的单态(例)设计模式
a.构造方法私有 private
b.生成静态的对象
c.在类的外部调用该类的某个静态方法以返回类内部创建的对象

单态设计模式

class Person
{   String name;
     int x=0;
     public Person()     {  ++x; }
     public void print()
     {  System.out.println(x);  }
}
class Test-5.7
{    public static void main(String[] args)
     {    new Person().print();   //1
          new Person().print();    //1
          new Person().print();    //1
     }
}
class Person
{   String name;
     static int x=0;
     public Person()     {  ++x; } 
     public void print()
     {  System.out.println(x);  }
}
class Test-5.8
{    public static void main(String[] args)
     {    new Person().print();    //1
           new Person().print();    //2
           new Person().print();    //3
     }
}
class Person
{  String name;
   static int x=0;
   static Person p=new Person();   //b 
/*最好为private static Person p=new Person();*/
   private Person()    {  ++x; }   //a
   public  static Person getP()  //c
   {  return p; } }
class Test-5.9
{  public static void main(String[] args)
   {   Person p=Person.getP();
       Person p1=Person.getP();
       Person p2=Person.getP();
       System.out.println(p==p1);       //true
       System.out.println(p1==p2);}     //true
} //无论生成多少对象,内存中仅有一个对象

String类型

1 两种实例化: (1)直接赋值 ; (2)通过构造方法赋值
区别:内存变化不一样。
一个字符串常量就是一个String的匿名对象。一个字符串赋值后,内容不能改变,只能改变引用。
2 两种比较方式:== 和 equals() 和 compareTo (- 0 +)
3 字符串与字节转化(byte单位小于char)
(byte) public String(byte[] bytes)
4 字符串与字符的转换
public String(char[] value)
public String(char[] value,int offset,int count) //offset起始点,count数量
public charAt(int index)
public char[] toCharArray()//转化成字符数组
5 字符串的操作:截取一部分字符串
public String substring(int beginIndex)

字符串池

class Test-5.10
{  public static void main(String[] args)
   { String n1="huang";  //直接赋值,直接入池
     String n4="huang";
     String n2=new String(“huang”);//构造方法
     String n3=new String("huang");
     String n5=new String("huang").intern();
//入池操作
     String n6=new String("huang").intern();
     System.out.println(n1==n2);        //false
     System.out.println(n2==n3);        //false
     System.out.println(n1==n4);        //true
     System.out.println(n6==n5);        //true
     System.out.println(n1==n5);}       //true
}

在这里插入图片描述

常用String方法

class Test-5.11
{
   public static void main(String[] args)
   {
     String n="huang";  
     n+="guo"; 
//改变引用,不要用在循环里面,处理慢
     System.out.println(n);
     }
}
//(问:字符串改变了吗?有几个对象?)
//答:没有改变字符串,改变的只是引用;有3个对象
class Test-5.12
{
   public static void main(String[] args)
   {
     //if(args[0].equals("huang"))
      if("huang".equals(args[0])//字符串常量与字符串变量比较,应用字符串常量与变量比较,保证比较的正常运行
	System.out.println("OK");
      else		System.out.println("BU OK");
        }
}//最好采用别的键盘输入方式
class Test-5.14
{  public static void main(String[] args)
   {  String n="huang";
      char []a=n.toCharArray();//转化成一个个字符
      for(char x:a)
      { System.out.println(x); }//此处用较强for循环是不好的,用普通for循环即可
      //h  u  a  n  g
      String h=new String(a,1,2);
      System.out.println(h);
      //ua
      System.out.println(n.charAt(2)); }//a,打印字符串的第几个索引(0-h,1-u,2-a,3-n,4-g)
      //a
}
class Test-5.15
{
   public static void main(String[] args)
   {
       String n="huang";
       System.out.println(n.substring(0));      //huang 
       System.out.println(n.subString(2));      //ang
       System.out.println(n.subString(2,3));    //a
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值