Java之类与对象。最为基础最为重要,学不好后面不要学了

1.类和对象的概念:面向对象就是从对象的角度观察,了解,认识世界。每一个对象都有两个特征,
一个是属性,一个是行为
2.类是对同一类对象的属性和行为的封装
的需要而抽象;抽象时应该按照人们的思维习惯进行抽象,抽象之后应该进行封装;
4.继承:继承是在已有类基础上生存新类的过程。已有类是一般类,新类是特殊类。继承是实现对台戏的前提条件,所以继承具有承前启后的作用
5.多态:就是同一类对象表现出的不同行为
6.方法的定义与使用
(1)方法名是方法的标识,每一个方法都应该有一个名字;
(2)方法类型:方法是用来进行数据处理的,数据处理后应该将处理的结果返回给主调方法。返回的结果也是数据,也应该有类型;
7.对象的创建与表示
(1)声明对象名: 类名 对象名;
(2)创建对象: new 构造方法([实际参数]);
(3)对象的声明和创建可以用一条语句实现:Circle myCircle=new Circle();
例子:

package one;
class Circle{
double radius;
void setradius(double r) {
radius=r;
}
double area() {
return Math.PI*radius*radius;
}
double perimeter() {
return 2*Math.PI*radius;
}
}
public class one2{
public static void main(String[] args) {
double area,perim;
Circle circle=new Circle();
circle.setradius(10);
area=circle.area();
perim=circle.perimeter();
System.out.printf("perimeter:%.2f",perim);
System.out.printf("area:%.2f",area);
}
}

这里写图片描述

由以知,方法area()没有参数,它计算的是多大的面积呢?它必须通过对象调用circle.area(),
对象circle中的域radius是圆的半径,当通过对象调用方法area()时,方法area()是用对象circle
的半径计算circle的面积,方法area()所需要的数据来自于对象。如果是面向过程的C语言,定义area()方法时,area()方法必须带有参数。当这个方法被调用时,由这个参数向方法传递圆的半径值
8.this关键字
(1). this是对象名,是对象的别名,又称对象的引用
(2).this的作用范围在方法中,当通过一个对象调用一个方法·时,运行时系统会将当前对象的引用传递到方法中,在方法中就可以通过this访问这个对象
9.默认访问权限的域和方法
(1).使用访问权限时,一般应该将域定义为private
方法一般定义为public,让公有方法成为对象和外界进行数据交换的窗口如果类中的成员不希望被类体外其他的类通过对象被访问,而又希望能够被子类直接访问,即用protected
例子:

package one;
    class Circle{
        private double radius;
        private int x,y;
        public void setradius(double r) {
            this.radius=radius;
        }
        public void setXY(int x,int y) {
            this.x=x;
            this.y=y;
        }
        public void move(int x1,int x2) {
            x+=x1;  y+=x2;
        }
        public int getx() {
            return x;
        }
        public int gety() {
            return y;
        }
    }
    public class one2{
        public static void main(String[] args) {
            Circle circle=new Circle();
            circle.setradius(10);
            circle.setXY(15,20);
            System.out.println("circle的位置:"+circle.getx()+","+circle.gety());
        }
    }

这里写图片描述

其中最后一行如果改为System.out.println(“circle的位置:”+circle.x+”,”+circle.y);就错了。类体外对象访问公有成员,不能访问私有成员
10对象做方法的参数4
例子:

package one;
    class point{
        int x,y;
        public point(int x,int y) {
            this.x=x;
            this.y=y;
        }
        public void print() {
            System.out.println("("+x+","+y+")");
        }
    }
    public class one2{
        public static void main(String[] args) {
        point p=new point(10,15);
        System.out.printf("%36.f:", "调用前的坐标");
        p.print();
        move(p);
        System.out.printf("%36s:","调用前move后的坐标:");
        p.print();
        }
        static void move(point p) {
            p.x=p.x+5;
            p.y=p.y+10;
            System.out.printf("%36s:", "调用move后的坐标:");
            p.print();
            System.out.println();
        }
    }

11.组合类
• 组合类的定义与一般类的定义方式一样,只不过类的部分域或全部域不是基本数据类型数据,而是由其他类所定义的对象

• 例子:
package one;
    class point{
        private int x,y;
        public point(int x,int y) {
            this.x=x;
            this.y=y;
        }
        public point(point p) {
            x=p.x;
            y=p.y;
        }
        public int getx() {
            return x;
        }
        public int gety() {
            return y;
        }
        public void move(int x1,int x2) {
            x=x+x1;
            y=y+x2;
        }
    }
    class Circle{
        private double radius;
        private point center;
        public Circle(double radius,int x,int y) {
            this.radius=radius;
            center=new point(x,y);
        }
        public Circle(double radius,point p) {
            this.radius=radius;
            center=new point(p);
        }
        public double area() {
            return Math.PI*radius*radius;
        }
        public double perimeter() {
            return 2*Math.PI*radius;
        }
        public int getcenterx() {
            return center.getx();
        }
        public int getcentery() {
            return center.gety();
        }
        public void move(int x1,int x2) {
            center.move(x1, x2);
        }
    }
    public class one2{
        public static void main(String[] args) {
        int x=15,y=25;
        double radius=10;
        Circle circle1=new Circle(radius,x,y);
        System.out.printf("circle1的面积:%.2f", circle1.area());
        System.out.printf("circle1的周长:%.2f", circle1.perimeter());
        System.out.print("移动前的位置");
        System.out.println("("+circle1.getcenterx()+","+circle1.getcentery()+")");
        circle1.move(11, 12);
        System.out.print("移动后的位置");
        System.out.println("("+circle1.getcenterx()+","+circle1.getcentery()+")");
        point p=new point(-15,-25);
        radius=15;
        Circle circle2=new Circle(radius,x,y);
        System.out.printf("circle2的面积:%.2f", circle2.area());
        System.out.printf("circle2的周长:%.2f", circle2.perimeter());
        System.out.print("移动前的位置");
        System.out.println("("+circle2.getcenterx()+","+circle2.getcentery()+")");
        circle1.move(-11, -12);
        System.out.print("移动后的位置");
        System.out.println("("+circle2.getcenterx()+","+circle2.getcentery()+")");

        }
    }

这里写图片描述

12.package和import
• (1)package语句
• 包是对类进行管理的一种方法。另外,采用包管理类还可以避免重名问题。定义包用package语句,命名规则与标识符命名规则一样,建议包名用小写字母。 package语句必须是程序中的第一条语句,而且最多只能有一条package语句。如果一个源文件中没有package语句,则将当前源文件所在的文件当作包(默认包)
• 做代码步骤
• 第一点,先新建包java project,然后新建包,再在包中新建类。最后新建主类,主类是在java project上新建

这里写图片描述

13.对象数组
• (1) 对象数组的声明形式是: 类名 对象数组名[]; 例:Employe 雇员[]为对象数组分配内存空间:对象数组名=new 类名[数组长度]; 例:雇员=new Employe[3]; 一般进行合并:Employe 雇员[]=new Employe[3];
• (2) 上述中,还需要为每一个元素实例化,首先先构造方法,例:public Employe() {}然后实例化每一个元素,即for(int i=0;i<雇员.length;i++) {雇员[i]=new Employe();} 然后先设置属性的方法 例:public void set(String id,String name) {this.name=name; this.id=id;}其次就是初始化每一个对象元素 例:雇员[0].set(“0001”,”某人”); 最后输出 例:for(Employe employe:雇员)//输出每一个雇员的信息 { System.out.println(employe.tostring());}
• 例子:

package one;
    class Employe{
        private String id;
        private String name;
        private int age;
        private String vocation;
        public Employe() {}
        public Employe(String id,String name,int age,String vocation) {
            set(id,name,age,vocation);
        }
        public void set(String id,String name,int age,String vocation) {
            this.id=id;
            this.name=name;
            this.age=age;
            this.vocation=vocation;
        }
        public String toString() {
            String mess=id+","+name+","+age+","+vocation;
            return mess;
        }
    }
    public class one2{
        public static void main(String[] args) {
        Employe employe1[]=new Employe[3];
        int i;
        for(i=0;i<employe1.length;i++) {
            employe1[i]=new Employe();
        }
        employe1[0].set("0001", "张三", 50, "总经理");
        employe1[1].set("0002", "李四", 40, "经理");
        employe1[2].set("0003", "王五", 30, "员工");
        for(Employe employe:employe1) {
            System.out.println(employe.toString());
        }
        }
    }

这里写图片描述

(3)可以使用另一种方法。 类名 对象数组名[]={对象表列}
• 例子:

package one;
    class Employe{
        private String id;
        private String name;
        private int age;
        private String vocation;
        public Employe() {}
        public Employe(String id,String name,int age,String vocation) {
            set(id,name,age,vocation);
        }
        public void set(String id,String name,int age,String vocation) {
            this.id=id;
            this.name=name;
            this.age=age;
            this.vocation=vocation;
        }
        public String toString() {
            String mess=id+","+name+","+age+","+vocation;
            return mess;
        }
    }
    public class one2{
        public static void main(String[] args) {
        Employe employe1=new Employe("0001", "张三", 50, "总经理");
        Employe employe2=new Employe("0002", "李四", 40, "经理");
        Employe employe3=new Employe("0003", "王五", 30, "员工");
        Employe employe4[]= {employe1,employe2,employe3};
        output(employe4);
        }
        public static void output(Employe employe5[]) {
            for(Employe employe:employe5) {
                System.out.println(employe.toString());
            }
        }
    }

或者声明对象数组并直接初始化,初始化元素直接调用构造方法创建对象

public class one2{
        public static void main(String[] args) {
                Employe 雇员[]={new Employe("0001", "张三", 50, "总经理"),
                                                      new Employe("0002", "李四", 40, "经理"),
                                                      new Employe("0003", "王五", 30, "员工")};
               output(雇员)
                }
• 14.static修饰域
• 例子: 
package one;
    class Employe{
        private String id;
        private String name;
        private int age;
        private String vocation;
        static int count=0;
        public Employe() {}
        public Employe(String id,String name,int age,String vocation) {
            set(id,name,age,vocation);
        }
        public void set(String id,String name,int age,String vocation) {
            this.id=id;
            this.name=name;
            this.age=age;
            this.vocation=vocation;
        }
        public String toString() {
            String mess=id+","+name+","+age+","+vocation;
            return mess;
        }
        public void 签到() {
            System.out.println(name+"签到");
            count++;
        }
        public int 出勤人数() {
            return count;
        }
    }
    public class one2{
        public static void main(String[] args) {
               Employe 雇员[]={new Employe("0001", "张三", 50, "总经理"),
                                               new Employe("0002", "李四", 40, "经理"),
                                               new Employe("0003", "王五", 30, "员工")};
        ○ for(Employe employe:雇员) {
            employe.签到();
        }
        System.out.println("今日出勤人数(不同途径得到)人出勤");
        //可以通过任一个雇员访问方法得到
        System.out.println("今日公有"+雇员[0].出勤人数()+"人出勤");
        System.out.println("今日公有"+雇员[2].出勤人数()+"人出勤");
        //也可以通过对象直接访问方法得到
        //声明count时无访问限定词,所以同一包中可用对象名访问
        System.out.println("今日公有"+雇员[1].count+"人出勤");
        //也可以通过类名访问静态域
        System.out.println("今日公有"+Employe.count+"人出勤");
        }
    }

这里写图片描述

注意:其中函数前需加static,因为main函数要调用,因为main也是static
• 15.静态初始化器
• 当静态初始化器所在的类第一次被使用时,静态初始化器首先被调用,而且仅此一次而已

•  16.匿名类     
package one;
    class 交通工具{
        public void runing() {
            System.out.println("一个交通工具在运行");
        }
    }
    public class one2{
        public static void main(String[] args) {
                 交通工具  aplane;
                   aplane=new 交通工具() {
            public void runing() {
            System.out.println("一家飞机在空中飞行");
            }
        };
         交通工具  aship=new 交通工具() {
                public void runing() {
                    System.out.println("一家轮船在水中滑行");
                }
            };
            aplane.runing();
            aship.runing();
        }
    • }

这里写图片描述

在上面面程序中,一架飞机和一艘轮船都是交通工具,;但是是具体的交通工具,所以程序应该给出具体交通工具的实际运行状态。由于在程序中没有定义飞机类和轮船类,所以临时定义相应的匿名类并同时创建该类的对象,从而表现出具体交通工具的运行状态,匿名类实际上是某一类的子类,如上述程序中的两个匿名类都是交通工具类的子类,如果一个类在一个程序中只使用一次,则定义成匿名类比较合适。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值