07-27

package com.atguigu.java;

/*

 * 一、面向对象的三条主线

 * 1Java类及类的成员:属性、方法、构造器、代码块、内部类

 * 2、面向对象的三大特征:封装、继承、多态

 * 3、其他关键字

 *

 * 二、人把大象装进冰箱

 * 1、面向过程:强调的是功能行为,以函数为最小单位,考虑怎么做。

 * ① 打开冰箱

 * ② 抬起大象,塞进冰箱

 * ③、关门

 *

 * 2、面向对象: 强调具备功能的大象,以类/对象为最小单位,考虑谁来做。

 *

 * {

 *     打开(冰箱门){

 *        冰箱.开开()

 *  

 *

 *     抬起(大象){

 *          大象.进入(冰箱)

 *        }

 *        关闭(冰箱){

 *               冰箱.闭合();

 *           

 *            }

 *       

 *     }

 * }

 *

 * 冰箱{

 *     开开(){}

 *     闭合(){}

 * }

 * 大象{

 *     进入(冰箱){

 *     }

 *

 * }

 *

 * 三、类和对象

 * 类(class)和对象(object)是面向对象的核心概念。

 * 类是一类事物的描述,是抽象的】概念上的定义

 * 对象:是实际存在的该类事务的每个个体,因此也成为实例。

 *

 * 面向对象程序设计的重点是类的设计

 * 设计类就是设计类的成员

 *

 * 设计类,其实就是设计类的成员

 * 属性 = 成员变量=field =

 * 方法 = 成员方法=函数

 *

 *

 * */

public class OOPTest {

}

package com.atguigu.java;

/*

 * 举例

 * 1

 * public void eat(){}

 * public void sleep(int hour){}

 * public String getName(){}

 * public String getNation(String nation){}

 *

 * */

public class Customer {

}

class CustomerTest{

 

  //属性

  String name;

  int age;

  boolean isMale;

 

  //方法

  public void eat(){

     System.out.println("客户吃饭");

  }

 

  public void sleep(int hour){

    

     System.out.println("休息了"+ hour + "个小时");

    

  public String getName{

     return name;

  }

    

  public String getNation(String nation){

     String info = "" + nation;

     return info;

  }

    

    

    

    

    

    

    

    

  }

}

package com.atguigu.java;

/*

 * 一、设计类,其实就是设计类的成员

 * 属性 = 成员变量=field =

 * 方法 = 成员方法=函数

 *

 * 二、类和对象的使用

 * 1、创建类和对象的成员

 * 2、创建类的对象

 * 3、通过对象.属性对象.方法调用对象的结构

 *

 * 三、如果创建了一个类的多个对象,则每个对象都会独立的拥有一套类的属性

 * */

import javax.swing.plaf.synth.SynthSpinnerUI;

public class PersonTest {

  public static void main(String[] args) {

     //创建person类的对象

     Person p1 = new Person();

    

     //调用对象的结构:属性、方法

     //调用属性:对象,方法

    

     p1.name = "tom";

     p1.isMale = true;

     System.out.println(p1.name);

    

     //调用方法:对象,方法

    

     p1.eat();

     p1.sleep();

     p1.talk("chinese");

    

  }

}

class Person{

 

  //属性

  String  name;

  int age = 1;

  boolean isMale;

 

  //方法

  public void eat(){

     System.out.println("人吃饭");

  }

 

  public void sleep(){

     System.out.println("人睡觉");

  }

  public void talk(String language){

     System.out.println("人可以说话,用的是"+language);

  }

}

package com.atguigu.java;

import javax.swing.plaf.synth.SynthSpinnerUI;

/*

 * 类中属性的使用

 *

 *

 * 属性(成员变量)  vs  局部变量

 *

 * 1、相同点

 *     1.1定义变量的格式:数据类型 变量名 = 变量值

 *     1.2先声明,后使用

 *     1.3变量都有其对相应的作用域

 *

 *

 * 2、不同点

 *     2.1在;类中声明的位置不同

 *     属性:直接定义在类的一对{}

 *     局部变量 :声明在方法内、方法形参、代码块内、构造器形参、构造器内部的变量。

 *

 *     2.2关于权限修饰的不同

 *     属性:可以在声明属性时,指名其权限,使用权限修饰符。

 *     常用的权限修饰符 privatepublic、缺省、protected

 *     局部变量:不可使用权限修饰符

 *

 *

 *     2.3默认初始化值得情况:

 *     属性:类的属性、根据其类型,都有默认初始化值。

 *     整型(byteshortintlong): 0

 *     浮点型(float,double);0.0

 *     字符型(char);0

 *     布尔型(boolean);false

 *

 *     引用数据类型(类、接口、数组);null

 *

 *

 *     局部变量:没有默认话初始值

 *        意味调用局部变量之前,一定要显式赋值

 *    

 *

 *     2.4  在内存中加载的位置

 *     属性:加载到堆空间

 *     局部变量,加载到栈空间

 *  

 *     3.1关于权限修饰符:默认方法的权限修饰符都先使用public

 *          Java规定的四种权限修饰符:privatepublic、缺省、protected

 *

 *

 *     3.2返回值类型:有返回值  vs  没有返回值

 *        3.2.1

 *        如果方法有返回值,则必须在方法声明时,指定返回值的类型。。同时,方法中需要使用return

 *        关键字来返回指定类型的变量或常量

 *        如果方法没有返回值,则方法声明时,使用 void来表示。通常,没有返回值的方法,就不用使用return

 *        但是如果使用,只能“return”表示结束此方法的意思

 *

 *        3.2.2我们定义方法该不该有返回值

 *        ①题目要求

 *        ②凭经验

 *       

 *     3.3方法名:属于标识符,遵循标识符的规则和规范,见名知意

 *

 *     3.4形参列表:方法可以声明0个,1个或多个形参。

 *        3.4.1格式:数据类型形参 1 ,数据类型形参2  ...

 *       

 *        3.4.2我们定义方法时,该不该定义形参?

 *          ①题目要求

 *          ②凭经验

 *     3.5 方法体:方法功能的体现

 *       

 *

 *     4.return关键字的使用

 *        1.作用范围:使用在方法中

 *        1.作用:结束方法

 *            ②针对有返回值类型的方法,使用“return数据方法返回所要的数据

 *     5.方法的使用,可以调用当前类的属性或方法

 *          特殊的,方法A中又调用了A方法

 *

 *

 * */

public class UserTest {

 

}

class User{

  String name;

  int agee;

  boolean isMale;

 

  public void talk(String language){//局部变量

     System.out.println(language);

  }

  public void eat(){

  String food = "烙饼";

  System.out.println("爱吃"+ food);

  }

}

例题

package com.atguigu.exer;

public class Person {

  String name;

  int age;

  int sex;

 

  public void study(){

     System.out.println("studying");

  }

 

  public void showAge(){

     System.out.println(age);

  }

 

  public int addAge(int i){

     age +=i;

     return age;

  }

 

 

}

package com.atguigu.exer;

public class PersonTest {

 

 

  public static void main(String[] args) {

     Person p1 = new Person();

    

     p1.name = "tom";

     p1.age = 18;

     p1.sex = 1;

    

     p1.study();

    

     p1.showAge();

    

     int newAge = p1.addAge(2);

     System.out.println(p1.name + "的新年龄为" + newAge);

    

     System.out.println(p1.age);

    

    

    

    

    

    

  }

}

package com.atguigu.exer;

public class CircleTest {

  public static void main(String[] args) {

     Circle c1 = new Circle();

     c1.radius = 5;

     double area = c1.findArea();

     System.err.println(area);

  }

}

class Circle{

 

 

  double radius;

 

  public double findArea(){

     double area = Math.PI *radius*radius;

     return area;

  }

}

package com.atguigu.exer;

public class Exer3Test {

  public static void main(String[] args) {

    

     Exer3Test test = new Exer3Test();

//  

     //1

//   test.method();

    

     //2

//   int area = test.method();

//   System.out.println(area);

    

     //3

     int area = test.method(12, 10);

     System.out.println(area);

  }

 

 

 

  //1

// public int  method(){

//   for(int i =1 ;i <=10 ;i++){

//     for(int j = 1 ;j <= 8;j++){

//       System.out.print("*");

 

 

//     }

//     System.out.println();

//   }

//  

//   return 10* 8;

// }

//

 

  //2

// public void method(){

//   for(int i =1 ;i <=10 ;i++){

//     for(int j = 1 ;j <= 8;j++){

//       System.out.print("*");

//     }

//     System.out.println();

//   }

//  

//  

// }

 

 

  //3

  public int method(int m,int n){

     for(int i =1 ;i <=10 ;i++){

       for(int j = 1 ;j <= 8;j++){

         System.out.print("*");

       }

       System.out.println();

     }

     return m*n;

  }

 

}

package com.atguigu.exer;

public class StudentTest {

  public static void main(String[] args) {

    

     //声明Student类型的数组

     Student[] stu  = new Student[20];

    

    

     for(int i = 0;i < stu.length;i++){

       //给数组元素赋值

       stu[i] = new Student();

       //Student对象的属性赋值

       stu[i].number = i +1;

       stu[i].state = (int)(Math.random() * (6-1+1)+1);

       stu[i].score = (int)(Math.random() * (100-0+1));  

      

     }

    

     //遍历学生数组

     for(int i = 0;i<stu.length;i++){

       //if(stu[i].state ==3){

         System.out.println(stu[i].info());

       //}

     }

    

  System.out.println("**************************");

    

    

     for(int i = 0; i<stu.length;i++){

       if(stu[i].state ==3){

         System.out.println(stu[i].info());

       }

     }

    

  System.out.println("**************************");

  System.out.println("**************************");

  System.out.println("**************************");

    

     for(int i = 0; i < stu.length -1 ;i++){

       for(int j = 0;j < stu.length - 1 - i;j++){

         if(stu[j].score > stu[j+1].score){

            Student temp = stu[j];

            stu[j] = stu[j+1];

            stu[j+1] = temp;

         }

        

       }

     }

    

    

       for(int i = 0;i<stu.length;i++){

      

            System.out.println(stu[i].info());       //}

       }

    

    

    

  }

 

}

class Student{

 

  int number;//学号

  int state;//年级

  int score;//成绩

 

 

  public String info(){

     return "学号" + number + ",年级" + state + ",成绩" + score;

  }

 

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值