(邓爱萍)类 对象 课本

  • 一.类与对象

  • 对象
    1. 对象就是组成现实世界的个体,他们之间存在着错综复杂的关系。
  • 面向对象的分析
    1. 一些对象具有相似的特征
    2. 一些对象之间有相互作用
    3. 把这些对象以及对象之间的关系找出来。
    1. 把个体归纳为不同的类型。将个体分类的过程实质上就是面向对象技术中的创建类的过程。
    2. 类是对一组有相同特性(属性)和相同行为(方法)的对象的抽象。

 

public class Circle {

         double xPos,yPos,radius;

         public Circle(double x,double y,double r){

         xPos=x;

         yPos=y;

         radius=r;

         }

         double area(){

                   double s;

                   s=Math.PI*radius*radius;

                   return s;

         }

        

         double perimeter(){

                   double p;

                   p=Math.PI*2*radius;

                   return p;

         }

        

         void move(double destX,double destY){

                   xPos=destX+1;

                   yPos=destY+1;

         }

}

 

 

import java.util.Scanner;

 

public class CircleApp {

 

         public static void main(String[] args) {

                   // TODO Auto-generated method stub

double s,p;

 

Scanner sc=new Scanner(System.in);

int x,y,r;

x=sc.nextInt();

y=sc.nextInt();

r=sc.nextInt();

 

Circle c=new Circle(x,y,r);

  s=c.area();

  p=c.perimeter();

 

System.out.println(s+" "+p);

         }

}

 

  • 构造方法的 重载

 

import java.io.PrintStream;

public class Human {

         String name;

         char sex;

         int age;

         Human(String n)          { name =n; }

         Human(String n, char s)  {  name=n; sex=s;  }

        

         Human(String n,char s, int a) { name=n; sex=s; age=a;}

        

public static void main(String[]args){

         Human h1=new Human("Liuis");

         Human h2=new Human("Marry",'v');

         Human h3=new Human("Bob",'',20);

         System.out.printf("%s,%c,%d",h1.name,h1.sex, h1.age);

         System.out.println();

         System.out.printf("%s,%c,%d",h2.name,h2.sex, h2.age);

         System.out.println();

         System.out.printf("%s %c %d",h3.name,h3.sex, h3.age

}

}

(一)域的 定义

三.静态域    变量

1.静态域属于 整个类的 成员变量 它储存在类的内存区域的 公共存储单元中。

因此 该类的对象访问它,获取的都是相同的值

如果一个对象修改了静态域的值,静态域值的变化将反映在所有对象

2.没有被 static 修饰的域,保存在某个对象的内存空间,它是属于某个具---体对象的

如果创建了一个类的多个对象,那么域在不同对象中拥有不同的存储单元,它们是不同的

public class StaticFieldApp {

 

        int a=2;

        static double d=3.0;   静态 成员变量(静态域) 

        public static void main(String args[]){

                 StaticFieldApp app1=new StaticFieldApp();

                 StaticFieldApp app2=new StaticFieldApp();

                 System.out.println("app1.a="+app1.a+"\tapp1.d="+app1.d);

                 System.out.println("app2.a="+app2.a+"\tapp2.d="+app2.d);

                 System.out.println("StaticFieldApp.d="+StaticFieldApp.d);

        通过类名 直接访问 静态域        

       

                 app1.a=90;

                 app1.d=7.89;

                 System.out.println("对象app1的域改变后");

                 System.out.println("app1.a="+app1.a+"\tapp1.d="+app1.d);

                 System.out.println("app2.a="+app2.a+"\tapp2.d="+app2.d);

                 System.out.println("StaticFieldApp.d="+StaticFieldApp.d);

        }

 }

 

 

 

   非静态域 不能通过类名 直接访问

public class StaticFieldApp {

        int a=2;

        static double d=3.0;

        public static void main(String args[]){

                 StaticFieldApp app1=new StaticFieldApp();

                 StaticFieldApp app2=new StaticFieldApp();

                 System.out.println("app1.a="+app1.a+"\tapp1.d="+app1.d);

                 System.out.println("app2.a="+app2.a+"\tapp2.d="+app2.d);

                 System.out.println("StaticFieldApp.d="+StaticFieldApp.a);

        非静态域 不能通过类名 直接访问

                

                 app1.a=90;

                 app1.d=7.89;

                 System.out.println("对象app1的域改变后");

                 System.out.println("app1.a="+app1.a+"\tapp1.d="+app1.d);

                 System.out.println("app2.a="+app2.a+"\tapp2.d="+app2.d);

                 System.out.println("StaticFieldApp.d="+StaticFieldApp.d);

        }

 }

四,静态初始化器

 

public class StaticInit {

       static int a=1;

       static int b;

       static{

              a=30;

              b=a-10;

              c=90.0;

       }

       static double c;

       int d;

       StaticInit(int s) {  d=s; }

      

       public static void main(String[] args) {

              // TODO Auto-generated method stub

System.out.println(StaticInit.a+" "+StaticInit.b+" "+StaticInit.c);

 

 

StaticInit s=new StaticInit(666);

System.out.println(s.a+" "+s.b+" "+s.c+" "+s.d);     //30 20 90.0    666

 

System.out.println(StaticInit.a+" "+StaticInit.b+" "+StaticInit.c); //30  20 90.0

       }

}

 

五.final 最终域  最终类

 

public class FinalFieldApp {

    static final int a=9;

    static double b=7.8;

                 int cint d;

           FinalFieldApp(int x ,int y){c=x;   d=y;}

    public int add() { return a+c+d; }

   

    public static void main(String[] args) {

        // TODO Auto-generated method stub

FinalFieldApp f=new FinalFieldApp(1,2);

 System.out.println(FinalFieldApp.a+" "+f.add());    //   9    12

    }

}

 

最终类 不能被 继承

final class Testfinal{

}

 

 

public class SubTestfinal extends Testfinal{

    public SubTestfinal() {    }

    public static void main(String[] args) {    }

}

 

(二)方法的 创建

一.方法的返回值

package 类对象;

public class ReturnApp {

    int a; int b;

    public int add(){

        int s; s=a+b;

        return s;

    }

    void setValue(int x,int y){

        a=x; b=y;  }

    int getA(){ return a;}

    int getB(){ return b; }

   

    public static void main(String[]args){

       

        ReturnApp r=new ReturnApp();

        r.setValue(1, 2);

       

        int sum;

        sum=r.add();

System.out.println(sum+" "+r.a+" "+r.b+" "+r.getA()+" "+r.getB()); // 3 1 2 1 2

    }

}

 

  • 方法的 参数传递

 

public class Rectangle {

    double x;------------------------------ 域(成员变量)

    double y;------------------------------

    Rectangle(double a,double b){

          x=a;

          y=b;

    }

    double area(){

          double s;  局部变量s

          s=x*y;    //  xy 的乘积

          return s;

    }

    double multiply(double x,double y){      

//multiply()方法的参数名 ---所在Rectangle 域名--相同              

                //在方法内部,参数 将隐藏 同名的域

          double s;  //局部变量(与第9行不同)

          s=x*y;        // 方法 参数名 xy      

    

          return s;                  

         

    }

 

    public static void main(String[] args) {

        Rectangle r=new Rectangle(3,4);=========对象r

        System.out.println("面积为:"+r.area());

        System.out.println("multiply方法的调用结果为:"+r.multiply(10,20));

    }

}

 

使用this

public class Rectangle {

    double x;------------------------------ 域(成员变量)

    double y;------------------------------

    Rectangle(double a,double b){

           x=a;

           y=b;

    }

    double area(){

           double s;

           s=x*y;   域 x,y 的乘积

           return s;

    }

    double multiply(double x,double y){   // 方法 的 参数名 x,y

           double s;

           //s=x*y;

           s=this.x*this.y;   ======== //this 表示当前类对象 r

           return s;                   // this.x this.y对象 的域 x,y

          

    }

 

    public static void main(String[] args) {

        Rectangle r=new Rectangle(3,4);=========对象r

        System.out.println("面积为:"+r.area());

        System.out.println("multiply方法的调用结果为:"+r.multiply(10,20));

    }

}

 

 

  1. 赋值调用 传递方式

形参 7.8---26.52   实参的值 并没有改变

public class Functransfer {

   void changex(float x){

         System.out.println("x="+x);

         x*=3.4;

         System.out.println("x="+x);

   }

    public static void main(String[] args) {

        float d;

        d=7.8f;

        System.out.println("方法调用前d的值为:"+d);

 

        Functransfer ft=new Functransfer();

        ft.changex(d);

        System.out.println("方法调用后d的值为:"+d);

    }

}

  1. 引用传递 方式(形参 实参代表 内存中的 同一存储单元)

形参---引用数据类型-(类//数组类型),,将自动采用 引用方式 调用实参

 

 

  • 方法中的局部变量

 

1.局部变量的 作用域-从变量定义开始,,到方法的结束

类的成员变量 作用域- 整个类

 

public class LocalVarible {

  int i=2;

 

  void move(){

      int x=7,y=8,s;

      s=x*y;

      System.out.println(x+" "+y+" "+s);

      System.out.println(i);

  }

    public static void main(String[] args) {

        // TODO Auto-generated method stub

LocalVarible r=new LocalVarible();

 r.move();                                      //成员方法    7  8  56

                                                                  2

 System.out.println(r.i);             //成员变量               2

    }

}

 

 

  1. 域名-(全局变量)  局部变量名 相同

1)局部变量 将隐藏 同名的全局变量      个人书库  图书馆大库

2)使用关键字 this,,表明 全局变量

public class EqualVarible {

 

    int i=2;

 

    void move(){

       int i=6;

    System.out.println(i);                      //局部变量

    System.out.println(this.i);                    //全局变量

    }

    public static void main(String[] args) {

       // TODO Auto-generated method stub

EqualVarible r=new EqualVarible();

 r.move();

    }

}

 

 

 

  1. 静态方法

1)静态方法 只能访问 静态域,,不能使用 非静态的成员变量

public class TestStaticFunc {

   int x;

   static int getx(){

            return x;

   }

   static void setx(int t){

            x=t;

   }

}

 

2

 

public class TestStaticFunc15 {

   static int  x;       将x修改为静态变量(静态域)

   

   static int getx(){      //静态方法

      System.out.println("the value of  x is: "+x);

              

           return x;

   }

  

   static void setx(int t){   //静态方法

               x=t;

   }

    public static void main(String[] args) {// main方法

   

         TestStaticFunc15 ts=new TestStaticFunc15();

TestStaticFunc15类内 生成  TestStaticFunc15类的对象ts,并用对象ts 来引用 静态变量  

 也可以不用创建对象  直接使用 类名TestStaticFunc15 作为 方法名的前缀来调用 静态方法 

         ts.x=40;

        

         TestStaticFunc15.getx();

         ts.getx();

        

          TestStaticFunc15.x=78;

         

         TestStaticFunc15.getx();

         ts.getx();

    }

}

5.方法的 重载

 

1.方法名相同 

2.方法返回值类型相同

3.参数表相同 (参数的个数  顺序 参数的类型必须完全相同)          方法的覆盖

如果1方法名相同,3不同==这两个方法是不同的方法    方法的重载

 

public class GraphicsArea {

    double area(double r){  return (Math.PI*r*r);  }

     double area(double w, double h){return (w*h);  }

    double area(double x, double y,double z){return (z*(x+y)/2);}

 

    public static void main(String[] args) {

        GraphicsArea obj=new GraphicsArea();

        System.out.println("圆的面积是"+obj.area(3.0));

        System.out.println("长方形的面积是"+obj.area(3.0,2.0));

        System.out.println("梯形的面积是"+obj.area(4.0,8.0,6.0));

 

    }

}

 

编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。

 

import java.io.*;

public class OverLoading

{

       String strAdd(String str1,String str2) {return str1+str2;   }

        String strAdd(int int1,int int2) { return  String.valueOf(int1+int2);}

        String strAdd(float flt1,float flt2) {return  String.valueOf (flt1+flt2);}

   

  public static void main(String args[]) {

       String s1,s2,ss,si,sf;

       int i1,i2;

       float f1,f2;

       BufferedReader s=new  BufferedReader(new InputStreamReader(System.in));

       try{System.out.print ("输入第一个字符串:" );

           s1= s.readLine();

           System.out.print ("输入第二个字符串:" );

           s2= s.readLine();}

       catch(Exception e){ System.out.println(e.getMessage());}

       i1 = Integer.parseInt(s1);

       i2 = Integer.parseInt(s2);

       f1 = Float.parseFloat(s1);

       f2 = Float.parseFloat(s2);

       ss = strAdd(s1,s2);

       si = strAdd(i1,i2);

       sf = strAdd(f1,f2);

       System.out.println ("输入的二个字符串相加结果为:"+ss );

       System.out.println ("输入字符串转换为整数相加结果为:"+si );

       System.out.println ("输入字符串转换为浮点数相加结果为:"+sf );

    }

 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangchuang2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值