java基础_b站郭永峰教程课程记录116-130

116 成员变量和局部变量注意事项讲解

public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
//         局部变量名称可以和成员变量名称一样,在方法中使用的时候,采用的是就近原则
           Duck duck = new Duck();
           duck.name = "小黑";
           duck.speak();
     }
}
class Duck{
     String name;
     
     public void speak() {
           String name = "小白";
           System.out.println("我是一只可爱吖:" + name);
     }
}
117 方法的形式参数是类名的时候如何调用讲解
需要传递该类的对象
public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
           //创建对象
           Dog dog = new Dog();
           dog.name = "a";
           dog.age = 1;
           //调用print方法
//         print(Dog);//这是一个类,错误调用
           print(dog);//dog是一个对象
     }
     public static void print(Dog dog) {
           System.out.println(dog.name + ":" + dog.age);
     }
     
}
class Dog{
     String name;
     int age;
     
     public void name() {
           
     }
}

118  匿名对象

public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
//         匿名对象:没有给新创建的对象存储在一个变量里
           GirlFriend gf = new GirlFriend();
           gf.name = "oo";
           gf.hometown = "日本";
           gf.height = 1.68;
           gf.say();
           
//         直接打印对象,结果:lesson118.GirlFriend@6f539caf
           System.out.println(gf);
           
           new GirlFriend();//匿名对象
           System.out.println(new GirlFriend());
                      
     }
}
class GirlFriend{
     String name;
     String hometown;
     double height;
     
     public void say() {
           System.out.println("我是" + name + "来自" + hometown);
     }
}

119  匿名对象讲解02

120  练习题
121 水池的小学生编程题
     public static void main(String[] args) {
           // TODO Auto-generated method stub
//         A水池有154吨水,B有82吨,两水池每小时都排出2吨水,多少小时后,a水池的吨数是b水池的4倍。
//         test1();
           int a = 154;
           int b = 82;
           
           int hour = 1;
           while(true) {
                a -= 2;
                b -= 2;
                
                if(a == 4*b) {
                     System.out.println(hour + "小时");
                     break;
                }
                hour ++;
           }
     }
122 猜猜甲乙丙丁谁说谎
public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
           int i = 100;
           
           while (i<=500) {
//              System.out.println(i);
                //先求abc
                int a = i/100;
                int b = i%100/10;
                int c = i%10;
                
                //甲
                int j = i%8;//j=0代表整除
                //乙
                int y = i%9;
                //丙
                int bing = i%7;
                //丁
                int ding = a + b +c;
                
//              判断4种情况:分别是每一个人会说谎的情况
                if(y == 0 && bing == 0 && ding == 15) {
                     System.out.println("甲说谎");
                }else if (j == 0 && bing == 0 && ding == 15) {
                     System.out.println("yi说谎");
                }else if (j == 0 && y == 0 && ding == 15) {
                     System.out.println("bing说谎");
                }else if (j == 0 && y == 0 && bing == 0) {
                     System.out.println("ding说谎");
                }
                
                i++;
           }
     }
}
123 private封装讲解
public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
           //年龄不能赋值负数,也不能赋值为无穷大
//         封装的解决方案步骤:
//         把age属性声明为私有的;提供一个公共的访问方法来实现年龄的赋值
           Person p = new Person();
           p.name = "nana";
//         p.age = 23;
           p.setAge(20);
           p.speak();
           
           //获取年龄?提供一个get方法
           System.out.println("年龄:" + p.getAge());
     }
}
class Person{
     String name;
     private int age;//只能在类内使用,外界不能使用
     public int getAge() {
           return age;
     }
     public void setAge(int myAge) {
           if(myAge > 0 && myAge <= 150) {
                age = myAge;
           }else {
                System.out.println("年龄只能是1`150哦~");
           }
           
     }
     
     public void speak() {
           System.out.println("I'm" + name + "," + age);
     }
}

 

 124 this关键字讲解

public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
//         this关键字特点:代表当前对象的引用;应用场景:用来区别成员变量喝局部变量重名
           Employee em = new Employee();
           em.name = "刘教练";
           em.position = "瑜伽";
           
           em.say();
           
     }
}
//this和外面调用的变量指向同一地址,指的是同一对象
class Employee{
     String name;//成员变量(全局变量)
     String position;
     public void say() {
           String name = "胡教练";//局部变量
           //this.name:表示访问成员变量
           System.out.println("我是"+this.name+"从事" +  position);
     }
}
我是刘教练从事瑜伽
 
125 构造方法constructor概述讲解
public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
           Person p = new Person();
//         p.name = "小飞";
//         p.age = 50;
           p.say();
           
           Person p1 = new Person();
           p1.say();
           
           Person p2 = new Person("贾", 30);
           p2.say();
           
           Person p3 = new Person("马", 45);
           p3.say();
     }
}
//ctrl+d 删除行
class Person{
     String name;
     int age;
     //声明一个无参(空参)的构造方法
     public Person(){
           name = "小飞";
           age = 50;
     }
     //声明一个有参的构造方法
     public Person(String myName,int myAge) {
           name = myName;
           age = myAge;
     }
     
     public void say() {
           System.out.println("我是" + name + "今年" + age);
     }
}

126 构造方法的重载讲解

public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
//         构造方法的重载:是指方法名相同,与返回值类型无关(构造方法没有返回值),只看参数列表
           //第一种方式调用
           Person p1 = new Person();
           p1.name = "张飞";
           p1.age = 40;
           p1.gender = "男";
           p1.say();
           //第二种方法调用person【空参构造】
           Person p2 = new Person();
           p2.say();
           //第三种方法调用person【有参,2个】
           Person p3 = new Person("可爱多", 20);
           p3.say();
           //第三种方法调用person【有参,3个】
           Person p4 = new Person("可爱多", 20, "女");
           p4.say();
     }
}
class Person{
     String name;
     int age;
     String gender;
     
     public Person(String myName,int myAge,String myGender) {
           name = myName;
           age = myAge;
           gender = myGender;
     }
     
     public Person(String myName,int myAge) {
           name = myName;
           age = myAge;
     }
     
     public Person() {
           name = "吕布";
           age = 20;
           gender = "男";
     }
     
     public void say() {
           System.out.println(name + "," + age + "," + gender);
     }
}
 
127 构造方法的注意事项讲解
public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
           //如果没有给构造方法,系统会默认提供一个构造方法;如果给出了构造方法,系统默认不给提供无参的构造方法,必须自己写一个无参的才可以
           GirlFriend gf = new GirlFriend();
           gf.say();
           
           GirlFriend gf1 = new GirlFriend("林志玲", "台湾");
           gf1.say();
     }
}
class GirlFriend{
     String name;
     String hometown;
     
     public GirlFriend() {
     }
     
     public GirlFriend(String myName,String myHometown) {
           name = myName;
           hometown = myHometown;
     }
     
     public void say() {
           System.out.println(name + "," + hometown);
     }
}

128 正确的set和构造方法的书写讲解

public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
           //set/get方法,构造方法
           Person p1 = new Person();
           p1.name = "关公";
           p1.setAge(20);
           p1.say();
           
     }
}
class Person{
     String name;
     private int age;
     //以后都这么写
     public void setAge(int age) {
           this.age = age;
     }
     
//   public void setAge(int myAge) {
//         age = myAge;
//   }
     
     public void say() {
           // TODO Auto-generated method stub
           System.out.println("say");
     }
     
}

129 成员变量(属性)的三种赋值方式

public class Demo01 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
           //直接给属性赋值,比较少用
           Student stu1 = new Student();
           stu1.name = "马云";
           stu1.age = 40;
           stu1.say();
           //直接通过set方法给属性赋值【开发中常用,并且成员变量为private】
           Student stu2 = new Student();
           stu2.setName("马云");
           stu2.setAge(40);
           stu2.say();
           //构造方法,给对象中属性进行初始化。提供一个有参的构造方法就要提供一个无参的构造方法
           Student stu3 = new Student("马云", 30);
           stu3.say();
     }
}
class Student{
     String name;
     int age;
     
     public Student() {
           // TODO Auto-generated method stub
     }
     //快捷方式:空白右键source-using fields
     public Student(String name, int age) {
           super();
           this.name = name;
           this.age = age;
     }
     public void setName(String name) {
           this.name = name;
     }
     
     
     public int getAge() {
           return age;
     }
     public void setAge(int age) {
           this.age = age;
     }
     public String getName() {
           return name;
     }
     public void say() {
           // TODO Auto-generated method stub
           System.out.println(name + age);
     }
}
130 
 
posted @ 2020-08-06 16:51  娜娜_nana  阅读(171)  评论(0编辑  收藏  举报