尚硅谷Java入门视频教程第五章——面向对象编程(中)

article/2025/5/10 17:14:19

尚硅谷Java入门视频教程第五章——面向对象编程(中)

  • 第5章:面向对象编程(中)
    • 5.1 面向对象特征之二:继承性
    • 5.2 方法的重写(override/overwrite)
    • 5.3 四种访问权限修饰符
    • 5.4 关键字:super
    • 5.5 子类对象实例化过程
    • 5.6 面向对象特征之三:多态性
      • 5.6.1 多态性及其理解
      • 5.6.2 关键字:instanceof
    • 5.7 Object类的使用
      • 5.7.1 ==操作符与equals方法
      • 5.7.2 toString() 方法
    • 5.8 包装类的使用
      • 5.8.1 单元测试方法
      • 5.8.2 包装类

第5章:面向对象编程(中)

5.1 面向对象特征之二:继承性

面向对象的特征之二:继承性 why?
在这里插入图片描述
为什么要有继承?
多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,
那么多个类无需再定义这些属性和行为,只要继承那个类即可。

  • 继承性的好处:
    ① 减少了代码的冗余,提高了代码的复用性
    ② 便于功能的扩展
    ③ 为之后多态性的使用,提供了前提

  • 继承性的格式:
    class A extends B{}
    A:子类、派生类、subclass
    B:父类、超类、基类、superclass

    1. 体现:一旦子类A继承父类B以后,子类A中就获取了父类B中声明的所有的属性和方法。
      特别的,父类中声明为private的属性或方法,子类继承父类以后,仍然认为获取了父类中私有的结构。只有因为封装性的影响,使得子类不能直接调用父类的结构而已。
    2. 子类继承父类以后,还可以声明自己特有的属性或方法:实现功能的拓展。
      子类和父类的关系,不同于子集和集合的关系。
      extends:延展、扩展
  • Java中关于继承性的规定:

    1. 一个类可以被多个子类继承。
    2. Java中类的单继承性:一个类只能有一个父类
    3. 子父类是相对的概念。
    4. 子类直接继承的父类,称为:直接父类。间接继承的父类称为:间接父类
    5. 子类继承父类以后,就获取了直接父类以及所有间接父类中声明的属性和方法
      继承性规定
  • java.lang.Object类初识

    1. 如果我们没有显式的声明一个类的父类的话,则此类继承于java.lang.Object类
    2. 所有的java类(除java.lang.Object类之外)都直接或间接的继承于java.lang.Object类
    3. 意味着,所有的java类具有java.lang.Object类声明的功能。
  • 练习
    根据下图实现类。在CylinderTest类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积。
    例题
    Circle类:

    package com.atguigu.exer;public class Circle {private double radius;//半径public Circle() {radius = 1.0;}public void setRadiu(double radius) {this.radius = radius;}public double getRadius() {return this.radius;}//返回圆的面积 public double findArea() {return Math.PI * radius * radius;}
    }
    

    Cylinder类

    package com.atguigu.exer;public class Cylinder extends Circle{private double length;public Cylinder() {length = 1.0;}public double getLength() {return length;}public void setLength(double length) {this.length = length;}//返回圆柱体的体积public double findVolume() {return Math.PI * getRadius() * getRadius() * getLength();//或//return findArea() * getLength();}
    }

    CylinderTest类

    package com.atguigu.exer;public class CylinderTest {public static void main(String[] args) {Cylinder cy = new Cylinder();cy.setRadiu(2.1);cy.setLength(3.4);double area = cy.findArea();System.out.println("圆柱体的底面积为:" + area);double volume = cy.findVolume();System.out.println("圆柱体的体积为:" + volume);}}
    

5.2 方法的重写(override/overwrite)

  • 重写:
    子类继承父类以后,可以对父类中同名同参数的方法,进行覆盖操作。
  • 应用:重写以后,当创建子类对象以后,通过子类对象调用子父类中的同名同参数的方法时,实际执行的是子类重写父类的方法。
  • 重写的规定:
    方法的声明:
    权限修饰符  返回值类型  方法名(形参列表) throws 异常的类型{//方法体
    }
    
    约定俗成:子类中的叫重写的方法,父类中的叫被重写的方法
    1. 子类重写的方法的方法名和形参列表与父类被重写的方法的方法名和形参列表相同
    2. 子类重写的方法的权限修饰符不小于父类被重写的方法的权限修饰符
      特殊情况:子类不能重写父类中声明为private权限的方法
    3. 返回值类型
      父类被重写的方法的返回值类型是void,则子类重写的方法的返回值类型只能是void
      父类被重写的方法的返回值类型是A类型,则子类重写的方法的返回值类型可以是A类或A类的子类
      父类被重写的方法的返回值类型是基本数据类型(比如:double),则子类重写的方法的返回值类型必须是相同的基本数据类型(必须也是double)——与变量类型提升无关
    4. 子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型(具体放到异常处理时候讲)
      实际开发中一般直接将父类方法的方法声明与参数列表直接复制粘贴到子类方法中,或者使用Eclipse的提示功能进行重写,防止手打出错。

子类和父类中的同名同参数的方法要么都声明为非static的(考虑重写),要么都声明为static的(不是重写)。


  • 面试题:区分方法的重载与重写:
    1. 二者的概念:
      ① 重载:
      同一个类中,方法名相同但参数列表不同(参数类型与数量)的方法之间构成重载。
      构造器也可以重载。
      ② 重写:
      子类继承父类以后,可以对父类中同名同参数的方法,进行覆盖操作。
      构造器不可以重写。

    2. 重载与重写的具体规则:

    3. 从编译和运行的角度看:
      ① 重载:
      不表现为多态性,编译器根据方法不同的参数表,对同名方法的名称做修饰。对于编译器而言,这些同名方法就成了不同的方法。它们的调用地址在编译期就绑定了。Java的重载是可以包括父类和子类的,即子类可以重载父类的同名不同参数的方法。所以:对于重载而言,在方法调用之前,编译器就已经确定了所要调用的方法,这称为 “早绑定”或“静态绑定”
      ② 重写:
      表现为多态性(Person p = new Man;p.eat();),只有等到方法调用的那一刻,解释运行器才会确定所要调用的具体方法,这称为==“晚绑定”或“动态绑定”==


  • 例题:对5.1节中的例题的Cylinder类进行修改,重写Circle类中的findArea方法,使得其返回圆柱体的表面积。
    Cylinder类:
    package com.atguigu.exer;public class Cylinder extends Circle{private double length;public Cylinder() {length = 1.0;}public double getLength() {return length;}public void setLength(double length) {this.length = length;}//返回圆柱体的体积public double findVolume() {return Math.PI * getRadius() * getRadius() * getLength();}//返回圆柱体的表面积@Overridepublic double findArea() {return Math.PI * getRadius() * getRadius() * 2 + 2 * Math.PI * getRadius() * getLength();}
    }
    

5.3 四种访问权限修饰符

访问权限修饰符

5.4 关键字:super

  • super理解为:父类的
  • super可以用来调用:属性、方法、构造器
  • super的使用:调用属性和方法
    1. 可以在子类的方法或构造器中。通过使用"super.属性"或"super.方法"的方式,显式的调用
      父类中声明的属性或方法。但是,通常情况下,我们习惯省略"super."
    2. 特殊情况:当子类和父类中定义了同名的属性时,我们要想在子类中调用父类中声明的属性,则必须显式的
      使用"super.属性"的方式,表明调用的是父类中声明的属性。
    3. 特殊情况:当子类重写了父类中的方法以后,我们想在子类的方法中调用父类中被重写的方法时,则必须显式的
      使用"super.方法"的方式,表明调用的是父类中被重写的方法。

Cylinder类中求圆柱体的体积时,体积=底面积*高,此时要用的findArea为父类Circle中定义的求圆的面积而不是子类Cylinder类重写的求表面积,因此使用super关键字

package com.atguigu.exer1;public class Cylinder extends Circle{private double length;//高public Cylinder(){length = 1.0;}public double getLength() {return length;}public void setLength(double length) {this.length = length;}//返回圆柱的体积public double findVolume(){
//		return Math.PI * getRadius() * getRadius() * getLength();return super.findArea() * getLength();}@Overridepublic double findArea() {//返回圆柱的表面积return Math.PI * getRadius() * getRadius() * 2 + 2 * Math.PI * getRadius() * getLength();}}
  • super调用构造器
    1. 我们可以在子类的构造器中显式的使用"super(形参列表)"的方式,调用父类中声明的指定的构造器
    2. "super(形参列表)"的使用,必须声明在子类构造器的首行
    3. 我们在类的构造器中,针对于"this(形参列表)"或"super(形参列表)"只能二选一,且必须二选一,不能同时出现
    4. 在构造器的首行,没有显式的声明"this(形参列表)“或"super(形参列表)”,则默认调用的是父类中空参的构造器:super()
    5. 在类的多个构造器中,至少有一个类的构造器中使用了"super(形参列表)",调用父类中的构造器
      (如果一个类中有n个构造器,则最多有 n - 1构造器中使用了"this(形参列表)”,另一个则为"super(形参列表)")

父类Circle:

package com.atguigu.exer;public class Circle {private double radius;//半径//	public Circle() {
//		radius = 1.0;
//	}public Circle(double radius){this.radius = radius;}public void setRadiu(double radius) {this.radius = radius;}public double getRadius() {return this.radius;}//返回圆的面积 public double findArea() {return Math.PI * radius * radius;}
}

子类Cylinder:

package com.atguigu.exer1;public class Cylinder extends Circle{private double length;//高public Cylinder(){length = 1.0;}public double getLength() {return length;}public void setLength(double length) {this.length = length;}//返回圆柱的体积public double findVolume(){
//		return Math.PI * getRadius() * getRadius() * getLength();return super.findArea() * getLength();}@Overridepublic double findArea() {//返回圆柱的表面积return Math.PI * getRadius() * getRadius() * 2 + 2 * Math.PI * getRadius() * getLength();}}

当Circle类中的空惨构造器注释时,子类Cylinder的构造器会报错。
报错信息
原因分析:
在父类Circle中有非空参的构造器,因此系统不再提供空参的构造器,在子类Cylinder的构造器中没有显式的使用的构造器,因此默认使用super(),但是父类中没有空参的,因此报错。
当子类Cylinder的空参构造器注释,系统在进行类的声明时仍然会有一个默认的空参构造器,其中仍然会默认的调用super(),因此也会报错。
报错信息

5.5 子类对象实例化过程

  • 思考:
    1. 为什么super(…)和this(…)调用语句不能同时在一个构造器中出现?
      两者在使用时都必须在构造器的第一行
    2. 为什么super(…)或this(…)调用语句只能作为构造器中的第一句出现?
  • 子类对象实例化的全过程:
    1. 从结果上来看:(继承性)
      子类继承父类以后,就获取了父类中声明的属性或方法。
      创建子类的对象,在堆空间中,就会加载所有父类中声明的属性。

    2. 从过程上来看:
      当我们通过子类的构造器创建子类对象时,我们一定会直接或间接的调用其父类的构造器,进而调用父类的父类的构造器,…
      直到调用了java.lang.Object类中空参的构造器为止。正因为加载过所有的父类的结构,所以才可以看到内存中有父类中的结构,子类对象才可以考虑进行调用。
      子类对象实例化过程

明确:虽然创建子类对象时,调用了父类的构造器,但是自始至终就创建过一个对象,即为new的子类对象。

  • 例题:例题
package com.atguigu.exer2;public class Account {private int id;protected double balance;private double annualInterestRate;//	public Account() {
//		super();
//	}public Account(int id, double balance, double annualInterestRate) {super();this.id = id;this.balance = balance;this.annualInterestRate = annualInterestRate;}public int getId() {return id;}public void setId(int id) {this.id = id;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}public double getAnnualInterestRate() {return annualInterestRate;}public void setAnnualInterestRate(double annualInterestRate) {this.annualInterestRate = annualInterestRate;}public double getMonthlyInterest() {return annualInterestRate / 12;}public void withdraw (double amount) {if(amount > balance) {System.out.println("余额不足!");}else {balance -= amount;}}public void deposit (double amount) {if(amount > 0) {balance += amount;}}
}

例题2

package com.atguigu.exer2;public class CheckAccount extends Account{private double overdraft;public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {super(id, balance, annualInterestRate);this.overdraft = overdraft;}public double getOverdraft() {return overdraft;}public void setOverdraft(double overdraft) {this.overdraft = overdraft;}@Overridepublic void withdraw(double amount) {if(amount <= balance) {balance -= amount;}else {double money = amount - balance;if(overdraft >= money) {balance = 0;overdraft -= money;}else {System.out.println("超过可透支额的限额!");}}}	
}
package com.atguigu.exer2;public class CheckAccountTest {public static void main(String[] args) {CheckAccount checkAccount = new CheckAccount(1122, 20000, 0.045, 5000);checkAccount.withdraw(5000);System.out.println("您的账户余额为:" + checkAccount.balance);System.out.println("您的可透支额:" + checkAccount.getOverdraft());System.out.println();checkAccount.withdraw(18000);System.out.println("您的账户余额为:" + checkAccount.balance);System.out.println("您的可透支额:" + checkAccount.getOverdraft());System.out.println();checkAccount.withdraw(3000);System.out.println("您的账户余额为:" + checkAccount.balance);System.out.println("您的可透支额:" + checkAccount.getOverdraft());}
}

5.6 面向对象特征之三:多态性

5.6.1 多态性及其理解

  • 多态性:

    1. 理解多态性:可以理解为一个事物的多种形态。
    2. 何为多态性:
      对象的多态性:父类的引用指向子类的对象(或子类的对象赋给父类的引用)
    3. 多态的使用:虚拟方法调用
    4. 子类中定义了与父类同名同参数的方法,在多态情况下,将此时父类的方法称为虚拟方法,父类根据赋给它的不同子类对象,动态调用属于子类的该方法。这样的方法调用在编译期是无法确定的。
      在编译期,只能调用父类中声明的方法,但在运行期,我们实际执行的是子类重写父类的方法。即,方法的调用是在运行时确定的——动态绑定
      总结:编译,看左边;运行,看右边。
    5. 多态性的使用前提① 类的继承关系 ② 方法的重写
    6. 对象的多态性,只适用于方法,不适用于属性(编译和运行都看左边)

    定义了一个父类Preson以及它的两个子类Man和Woman:

    package com.atguigu.java4;public class Person {String name;int age;int id = 1001;public void eat(){System.out.println("人:吃饭");}public void walk(){System.out.println("人:走路");}}
    
    package com.atguigu.java4;public class Man extends Person{boolean isSmoking;int id = 1002;public void earnMoney(){System.out.println("男人负责挣钱养家");}public void eat(){System.out.println("男人多吃肉,长肌肉");}public void walk(){System.out.println("男人霸气的走路");}}
    
    package com.atguigu.java4;public class Woman extends Person{boolean isBeauty;public void goShopping(){System.out.println("女人喜欢购物");}public void eat(){System.out.println("女人少吃,为了减肥");}public void walk(){System.out.println("女人窈窕的走路");}
    }
    

    多态性的说明:

    package com.atguigu.java4;public class PersonTest {public static void main(String[] args) {Person p1 = new Person();p1.eat();Man man = new Man();man.eat();man.age = 25;man.earnMoney();//*************************************************System.out.println("*******************");//对象的多态性:父类的引用指向子类的对象Person p2 = new Man();
    //		Person p3 = new Woman();//多态的使用:当调用子父类同名同参数的方法时,实际执行的是子类重写父类的方法 ---虚拟方法调用p2.eat();p2.walk();//		p2.earnMoney();//不可以调用父类没有声明的方法System.out.println(p2.id);//1001(与属性无关,属性仍然是父类中的)}
    }
    
  • 多态性的理解:

    1. 减少冗余代码(重载方法)
    2. 增加方法的通用性
    3. 规范代码操作
    4. 抽象类、接口的使用肯定体现了多态性(抽象类、接口不能实例化,因此在使用时只能创建其子类的对象)
package com.atguigu.java4;import java.sql.Connection;//多态性的使用举例一:
public class AnimalTest {public static void main(String[] args) {AnimalTest test = new AnimalTest();test.func(new Dog());test.func(new Cat());}public void func(Animal animal){//Animal animal = new Dog();animal.eat();animal.shout();if(animal instanceof Dog){Dog d = (Dog)animal;d.watchDoor();}}//减少重载方法的编写
//	public void func(Dog dog){
//		dog.eat();
//		dog.shout();
//	}
//	public void func(Cat cat){
//		cat.eat();
//		cat.shout();
//	}
}class Animal{public void eat(){System.out.println("动物:进食");}public void shout(){System.out.println("动物:叫");}}class Dog extends Animal{public void eat(){System.out.println("狗吃骨头");}public void shout(){System.out.println("汪!汪!汪!");}public void watchDoor(){System.out.println("看门");}
}
class Cat extends Animal{public void eat(){System.out.println("猫吃鱼");}public void shout(){System.out.println("喵!喵!喵!");}
}//举例二:class Order{//增加方法的通用性:Object中可能定义很多不同类型的对象都需要使用的通用方法public void method(Object obj){}
}//举例三:
class Driver{//规范操作:不同的数据库类型具体代码不同,但要实现的功能相同,因此使用java.sql.Connection创建连接,写好各个方法的功能,具体实现时调用各个类型数据库自己实现功能的代码public void doData(Connection conn){//conn = new MySQlConnection(); / conn = new OracleConnection();//规范的步骤去操作数据
//		conn.method1();
//		conn.method2();
//		conn.method3();}
}
  • 多态小结
    1. 多态作用:
      提高了代码的通用性,常称作接口重用
    2. 前提:
      ① 需要存在继承或者实现关系
      ② 有方法的重写
    3. 成员方法:
      编译时:要查看引用变量所声明的类中是否有所调用的方法。
      运行时:调用实际new的对象所属的类中的重写方法。
    4. 成员变量:
      不具备多态性,只看引用变量所声明的类

5.6.2 关键字:instanceof

  • 问题引入:
    有了对象的多态性以后,内存中实际上是加载了子类特有的属性和方法的,但是由于变量声明为父类类型,导致编译时,只能调用父类中声明的属性和方法。子类特有的属性和方法不能调用。
    如何调用子类的属性和方法?——向下转型:使用强制类型转换符
    类型转换
    但是向下转型存在一定风险,可能会导致运行错误(一个父类可能包含多个子类,子类之间进行向下转型编译时不报错,但运行时会报错),因此在转换前应该对变量的具体类型进行判断 -> instanceof关键字
  • instanceof关键字的使用
    ① a instanceof A:判断对象a是否是类A的实例。如果是,返回true;如果不是,返回false。
    ② 如果 a instanceof A返回true,则 a instanceof B也返回true。 其中,类B是类A的父类
    ③ 要求a所属的类与类A必须是子父类的关系,否则编译错误。
package com.atguigu.java;import java.util.Date;public class PersonTest {public static void main(String[] args) {//对象的多态性:父类的引用指向子类的对象Person p2 = new Man();//不能调用子类所特有的方法、属性:编译时,p2是Person类型。
//		p2.earnMoney();//报错
//		p2.isSmoking = true;//报错//如何才能调用子类特有的属性和方法?//向下转型:使用强制类型转换符。Man m1 = (Man)p2;m1.earnMoney();m1.isSmoking = true;//使用强转时,可能出现ClassCastException的异常。Man类型不可以转换为Woman类型
//		Woman w1 = (Woman)p2;
//		w1.goShopping();/** instanceof关键字的使用:a instanceof A:判断对象a是否是类A的实例。如果是,返回true;如果不是,返回false。* *  使用情境:为了避免在向下转型时出现ClassCastException的异常,我们在向下转型之前,先进行instanceof的判断,一旦返回true,就进行向下转型。如果返回false,不进行向下转型。*  *  如果 a instanceof A返回true,则 a instanceof B也返回true.*  其中,类B是类A的父类。*/if(p2 instanceof Woman){Woman w1 = (Woman)p2;w1.goShopping();System.out.println("******Woman******");}if(p2 instanceof Man){Man m2 = (Man)p2;m2.earnMoney();System.out.println("******Man******");}if(p2 instanceof Person){System.out.println("******Person******");}if(p2 instanceof Object){System.out.println("******Object******");}//		if(p2 instanceof String){
//			
//		}//练习://问题一:编译时通过,运行时不通过//举例一:
//		Person p3 = new Woman();
//		Man m3 = (Man)p3;//举例二:
//		Person p4 = new Person();
//		Man m4 = (Man)p4;//问题二:编译通过,运行时也通过Object obj = new Woman();Person p = (Person)obj;//问题三:编译不通过
//		Man m5 = new Woman();//		String str = new Date();//编译过,运行不通过
//		Object o = new Date();
//		String str1 = (String)o;}
}
  • 练习:
    1. 判断JAVA文件的输出结果:

      package com.atguigu.exer;class Base {int count = 10;public void display() {System.out.println(this.count);}
      }class Sub extends Base {int count = 20;public void display() {System.out.println(this.count);}
      }public class FieldMethodTest {public static void main(String[] args) {Sub s = new Sub();System.out.println(s.count);//20s.display();//20Base b = s;//多态性//==:对于引用数据类型来讲,比较的是两个引用数据类型变量的地址值是否相同System.out.println(b == s);//trueSystem.out.println(b.count);//10b.display();//20}
      }
      

      若子类重写了父类方法,就意味着子类里定义的方法彻底覆盖了父类里的同名方法,系统将不可能把父类里的方法转移到子类中:编译看左边,运行看右边
      对于实例变量(属性)则不存在这样的现象,即使子类里定义了与父类完全相同的实例变量,这个实例变量依然不可能覆盖父类中定义的实例变量:编译运行都看左边

    2. 编写InatanceTest类例题2

      package com.atguigu.exer;public class InstanceTest {public static void main(String[] args) {InstanceTest test = new InstanceTest();test.method(new Student());}public void method(Person e){//(1):虚拟方法调用String info = e.getInfo();System.out.println(info);//(2):instanceof判断//方式一
      //		if(e instanceof Graduate){
      //			System.out.println("a graduated student");
      //			System.out.println("a student");
      //			System.out.println("a person");
      //		}else if(e instanceof Student){
      //			System.out.println("a student");
      //			System.out.println("a person");
      //		}else{
      //			System.out.println("a person");
      //		}//方式二if(e instanceof Graduate){System.out.println("a graduated student");}if(e instanceof Student){System.out.println("a student");}if(e instanceof Person){System.out.println("a person");}}
      }class Person {protected String name = "person";protected int age = 50;public String getInfo() {return "Name: " + name + "\n" + "age: " + age;}
      }class Student extends Person {protected String school = "pku";public String getInfo() {return "Name: " + name + "\nage: " + age + "\nschool: " + school;}
      }class Graduate extends Student {public String major = "IT";public String getInfo() {return "Name: " + name + "\nage: " + age + "\nschool: " + school + "\nmajor:" + major;}
      }
      
    3. 定义三个类,父类GeometricObject代表几何形状,子类Circle代表圆形,MyRectangle代表矩形。定义一个测试类GeometricTest,编写equalsArea方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术),编写displayGeometricObject方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)。
      在这里插入图片描述
      父类:

      package com.atguiau.exer1;public class GeometricObject {protected String color;protected double weight;protected GeometricObject(String color, double weight) {super();this.color = color;this.weight = weight;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}public double findArea() {return 0.0;}}
      

      子类1:

      package com.atguiau.exer1;public class Circle extends GeometricObject {private double radius;protected Circle(double radius, String color, double weight) {super(color, weight);this.radius = radius;}public double getRadius() {return radius;}public void setRadius(double radius) {this.radius = radius;}@Overridepublic double findArea() {return Math.PI * radius * radius;}}
      

      子类2:

      package com.atguiau.exer1;public class MyRectangle extends GeometricObject {private double width;private double height;public MyRectangle(String color, double weight, double width, double height) {super(color, weight);this.width = width;this.height = height;}public double getWidth() {return width;}public void setWidth(double width) {this.width = width;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}@Overridepublic double findArea() {return width * height;}
      }
      

      测试类:

      package com.atguiau.exer1;public class GeometricTest {public static void main(String[] args) {GeometricTest geomTest = new GeometricTest();Circle c1 = new Circle(1, "blue", 2);Circle c2 = new Circle(1, "white", 3);double area1 = geomTest.displayGeometricObject(c1);System.out.println("面积为" + area1);double area2 = geomTest.displayGeometricObject(c2);System.out.println("面积为" + area2);double area3 = geomTest.displayGeometricObject(new MyRectangle("yellow", 2, 1, 2));System.out.println("面积为" + area3);boolean isEquals = geomTest.equalsArae(c1, c2);System.out.println("面积是否相等:" + isEquals);}public boolean equalsArae(GeometricObject o1, GeometricObject o2) {return o1.findArea() == o2.findArea();}public double displayGeometricObject(GeometricObject o) {return o.findArea();}
      }
      

  1. 面试题:多态是编译时行为还是运行时行为?如何证明?

    运行时行为。
    证明:

    package com.atguigu.java5;import java.util.Random;class Animal  {protected void eat() {System.out.println("animal eat food");}
    }class Cat  extends Animal  {protected void eat() {System.out.println("cat eat fish");}
    }class Dog  extends Animal  {public void eat() {System.out.println("Dog eat bone");}}class Sheep  extends Animal  {public void eat() {System.out.println("Sheep eat grass");}}public class InterviewTest {public static Animal  getInstance(int key) {switch (key) {case 0:return new Cat ();case 1:return new Dog ();default:return new Sheep ();}}public static void main(String[] args) {int key = new Random().nextInt(3);System.out.println(key);Animal  animal = getInstance(key);animal.eat();}
    }
    

    若此段代码不运行起来,我们永远不知道输出结果是什么。

  2. 笔试题:判断以下程序的输出结果

    package com.atguigu.exer;//考查多态的笔试题目:
    public class InterviewTest1 {public static void main(String[] args) {Base1 base = new Sub1();//多态性,父类只能调用父类中定义过的子类重写的方法base.add(1, 2, 3);//sub_1Sub1 s = (Sub1)base;s.add(1,2,3);//sub_2}
    }class Base1 {public void add(int a, int... arr) {System.out.println("base1");}
    }class Sub1 extends Base1 {public void add(int a, int[] arr) {System.out.println("sub_1");}public void add(int a, int b, int c) {System.out.println("sub_2");}
    }
    

    int... arrint[] arr在java看来是相同的,因此子类Sub1中定义的add(int a, int[] arr)是对父类Base1中定义的add(int a, int… arr)的重写(同名同参数的方法)。在调用base.add(1, 2, 3)时,父类的引用指向子类的对象,执行的应为子类的方法(sub_1)。add(int a, int b, int c)不能与前两个看做同类型的,因此不会改变base.add(1, 2, 3)的执行结果。
    ② 但是,将base强制转换为Sub1类型后,在执行时,只在Sub1类内部执行,此时,会优先执行参数数量精确匹配的方法(sub_2)。


5.7 Object类的使用

  • java.lang.Object类
    1. Object类是所有Java类的根父类
    2. 如果在类的声明中未使用extends关键字指明其父类,则默认父类为java.lang.Object类
    3. Object类中的功能(属性、方法)就具有通用性。
      属性:无
      方法:equals() / toString() / getClass() /hashCode() / clone() / finalize()
      wait() 、 notify()、notifyAll()
      finalize()是在系统立即回收前通知对象调用的方法,System.gc();//强制性释放空间,子类重写此方法,可在释放对象前进行某些操作。但一般不提倡程序员主动调用此方法。
    4. Object类只声明了一个空参的构造器

5.7.1 ==操作符与equals方法

  • 面试题: == 和 equals() 区别
  1. == 运算符的使用:

    1. == 是一个运算符
    2. 可以使用在基本数据类型变量和引用数据类型变量中
    3. 如果比较的是基本数据类型变量:比较两个变量保存的数据是否相等。(不一定类型要相同)
      如果比较的是引用数据类型变量:比较两个对象的地址值是否相同.即两个引用是否指向同一个对象实体
    4. 补充: == 符号使用时,必须保证符号左右两边的变量类型一致。
  2. equals()方法的使用:

    1. equals()是一个方法,而非运算符

    2. 只能适用于引用数据类型

    3. Object类中equals()的定义:

      public boolean equals(Object obj) {return (this == obj);}
      

      说明:Object类中定义的equals()和==的作用是相同的:比较两个对象的地址值是否相同.即两个引用是否指向同一个对象实体

    4. String、Date、File、包装类等都重写了Object类中的equals()方法。重写以后,比较的不是两个引用的地址是否相同,而是比较两个对象的"实体内容"是否相同

    5. 通常情况下,我们自定义的类如果使用equals()的话,也通常是比较两个对象的"实体内容"是否相同。那么,我们就需要对Object类中的equals()进行重写。
      重写的原则:比较两个对象的实体内容是否相同。

      package com.atguigu.java1;import java.util.Date;public class EqualsTest {public static void main(String[] args) {//基本数据类型int i = 10;int j = 10;double d = 10.0;System.out.println(i == j);//trueSystem.out.println(i == d);//trueboolean b = true;
      //		System.out.println(i == b);//编译错误char c = 10;System.out.println(i == c);//truechar c1 = 'A';char c2 = 65;System.out.println(c1 == c2);//true//引用类型:Customer cust1 = new Customer("Tom",21);Customer cust2 = new Customer("Tom",21);System.out.println(cust1 == cust2);//false	//new了两个对象,地址值肯定不同String str1 = new String("atguigu");String str2 = new String("atguigu");System.out.println(str1 == str2);//false//不是想要的结果,需要重写System.out.println("****************************");System.out.println(cust1.equals(cust2));//false--->trueSystem.out.println(str1.equals(str2));//trueDate date1 = new Date(32432525324L);Date date2 = new Date(32432525324L);System.out.println(date1.equals(date2));//true}
      }
      
  • Customer类中equals()方法的重写:
    重写equals()方法的原则:
    在这里插入图片描述
    package com.atguigu.java1;public class Customer {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Customer() {super();}public Customer(String name, int age) {super();this.name = name;this.age = age;}//重写的原则:比较两个对象的实体内容(即:name和age)是否相同//手动实现equals()的重写@Overridepublic boolean equals(Object obj) {System.out.println("Customer equals()....");//比较地址值是否相等if (this == obj) {return true;}//判断类型是否一致if(obj instanceof Customer){Customer cust = (Customer)obj;//向下转型,为了使用子类的方法//比较两个对象的每个属性是否都相同//age是基本数据类型,使用==; name是引用数据类型,用equalsreturn this.age == cust.age && this.name.equals(cust.name);}return false;}
    }
    
    实际开发中:直接自动生成(Source -> Generate hasCode() and equals())
    package com.atguigu.java1;public class Customer {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Customer() {super();}public Customer(String name, int age) {super();this.name = name;this.age = age;}//自动生成的equals()@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Customer other = (Customer) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}
    }
    
  • 例题
    例题1

5.7.2 toString() 方法

  • Object类中toString()的使用:
    1. 大多数情况下,当我们输出一个对象的引用时,实际上就是调用当前对象的toString()
      当对象为null时,打印输出此对象时显示null,但如果使用toString()方法,会出现NullPointerException——println内部有保护机制
      print内部

    2. Object类中toString()的定义:

      public String toString() {return getClass().getName() + "@" + Integer.toHexString(hashCode());}```
      
    3. 像String、Date、File、包装类等都重写了Object类中的toString()方法。使得在调用对象的toString()时,返回"实体内容"信息

    4. 自定义类也可以重写toString()方法,当调用此方法时,返回对象的"实体内容"

      package com.atguigu.java1;import java.util.Date;public class ToStringTest {public static void main(String[] args) {Customer cust1 = new Customer("Tom",21);System.out.println(cust1.toString());//com.atguigu.java1.Customer@15db9742-->Customer[name = Tom,age = 21]System.out.println(cust1);//com.atguigu.java1.Customer@15db9742-->Customer[name = Tom,age = 21]String str = new String("MM");System.out.println(str);//MMDate date = new Date(4534534534543L);System.out.println(date.toString());//Mon Sep 11 08:55:34 GMT+08:00 2113}
      }
      
  • toString()方法的重写
    package com.atguigu.java1;public class Customer {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Customer() {super();}public Customer(String name, int age) {super();this.name = name;this.age = age;}//自动生成的equals()@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Customer other = (Customer) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}//手动实现
    //	@Override
    //	public String toString() {
    //		return "Customer[name = " + name + ",age = " + age + "]"; 
    //	}//自动实现@Overridepublic String toString() {return "Customer [name=" + name + ", age=" + age + "]";}
    }
    
  • 例题
    定义两个类,父类GeometricObject代表几何形状,子类Circle代表圆形。
    父类
    子类
    写一个测试类,创建两个Circle对象,判断其颜色是否相等;利用equals方法判断其半径是否相等;利用toString()方法输出其半径。
    父类GeometricObject:
    package com.atguigu.exer3;public class GeometricObject {protected String color;protected double weight;public GeometricObject() {super();this.color = "White";this.weight = 1.0;}public GeometricObject(String color, double weight) {super();this.color = color;this.weight = weight;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}
    }
    
    子类Circle:
    package com.atguigu.exer3;public class Circle extends GeometricObject {private double radius;public Circle() {super();this.radius = 1.0;}public double getRadius() {return radius;}public Circle(double radius) {super();this.radius = radius;}public Circle(double radius, String color, double weight) {super(color, weight);this.radius = radius;}public void setRadius(double radius) {this.radius = radius;}//求圆的面积public double findArea() {return Math.PI * radius * radius;}//重写equals方法,比较两个圆的半径是否相等,如相等,返回true。@Overridepublic boolean equals(Object obj) {if(this == obj){return true;}if(obj instanceof Circle) {Circle cir = (Circle)obj;return this.radius == cir.radius;}return false;}@Override//重写toString方法,输出圆的半径。public String toString() {return "Circle[radius = " + this.radius + "]";}}
    
    测试类 :
    package com.atguigu.exer3;public class CircleTest {public static void main(String[] args) {Circle c1 = new Circle(3.3);Circle c2 = new Circle(3.3, "White", 2.0);//color是字符串,需要用equals判断是否相等System.out.println("颜色是否相等:" + c1.getColor().equals(c2.color));System.out.println("半径是否相等:" + c1.equals(c2));System.out.println(c1);//Circle[radius = 3.3]System.out.println(c1.toString());//Circle[radius = 3.3]}
    }
    

5.8 包装类的使用

5.8.1 单元测试方法

  • 步骤:
    1. 选中当前工程 - 右键选择:build path - add libraries - JUnit 4 - 下一步
      (实际开发中只需在需要创建注解@Test后在进行导入即可)
    2. 创建Java类,进行单元测试。
      此时的Java类要求:① 此类是public的 ②此类提供公共的无参的构造器
    3. 此类中声明单元测试方法。
      此时的单元测试方法要求:方法的权限是public,没有返回值,没有形参
    4. 此单元测试方法上需要声明注解:@Test,并在单元测试类中导入:import org.junit.Test;
    5. 声明好单元测试方法以后,就可以在方法体内测试相关的代码。
    6. 写完代码以后,左键双击单元测试方法名,右键:run as - JUnit Test
      说明:
      ① 如果执行结果没有任何异常:绿条
      ② 如果执行结果出现异常:红条
    package com.atguigu.java2;import java.util.Date;import org.junit.Test;public class JUnitTest {int num = 10;@Testpublic void testEquals(){String s1 = "MM";String s2 = "MM";System.out.println(s1.equals(s2));//ClassCastException的异常
    //		Object obj = new String("GG");
    //		Date date = (Date)obj;System.out.println(num);show();}public void show(){num = 20;System.out.println("show()....");}@Testpublic void testToString(){String s2 = "MM";System.out.println(s2.toString());}
    }
    

5.8.2 包装类

  • 包装类(Wrapper)的使用:
    1. 为什么要有包装类?
      java针对八种基本数据类型提供了相应的引用类型—包装类(封装类),使得基本数据类型的变量具有类的特征,就可以调用类中的方法,Java才是真正的面向对象
      包装类
    2. 掌握的:基本数据类型、包装类、String三者之间的相互转换
      类型转换
      记住红色部分的方法即可。
      简易版:
      基本数据类型 <----------> 包装类:JDK 5.0新特性:自动装箱与自动拆箱
      基本数据类型、包装类 ------> String:调用String重载的ValueOf(Xxx xxx)
      String ------> 基本数据类型、包装类:调用包装类的parseXxx(String s)
      package com.atguigu.java2;import org.junit.Test;public class WrapperTest {//String类型 --->基本数据类型、包装类:调用包装类的parseXxx(String s)@Testpublic void test5(){String str1 = "123";//错误的情况:
      //		int num1 = (int)str1;
      //		Integer in1 = (Integer)str1;//可能会报NumberFormatException(String str1 = "123a")int num2 = Integer.parseInt(str1);System.out.println(num2 + 1);String str2 = "true1";boolean b1 = Boolean.parseBoolean(str2);System.out.println(b1);//false}//基本数据类型、包装类--->String类型:调用String重载的valueOf(Xxx xxx)@Testpublic void test4(){int num1 = 10;//方式1:连接运算String str1 = num1 + "";//方式2:调用String的valueOf(Xxx xxx)float f1 = 12.3f;String str2 = String.valueOf(f1);//"12.3"Double d1 = new Double(12.4);String str3 = String.valueOf(d1);System.out.println(str2);//"12.3"System.out.println(str3);//"12.4"}/** JDK 5.0 新特性:自动装箱 与自动拆箱*/@Testpublic void test3(){
      //		int num1 = 10;
      //		//基本数据类型-->包装类的对象
      //		method(num1);//自动装箱:基本数据类型 --->包装类int num2 = 10;Integer in1 = num2;//自动装箱boolean b1 = true;Boolean b2 = b1;//自动装箱//自动拆箱:包装类--->基本数据类型System.out.println(in1.toString());int num3 = in1;//自动拆箱}public void method(Object obj){System.out.println(obj);}//包装类--->基本数据类型:调用包装类Xxx的xxxValue()
      //	@Test
      //	public void test2(){
      //		Integer in1 = new Integer(12);
      //		
      //		int i1 = in1.intValue();
      //		System.out.println(i1 + 1);
      //		
      //		
      //		Float f1 = new Float(12.3);
      //		float f2 = f1.floatValue();
      //		System.out.println(f2 + 1);
      //	}//基本数据类型 --->包装类:调用包装类的构造器@Testpublic void test1(){//		int num1 = 10;
      		System.out.println(num1.toString());
      //		Integer in1 = new Integer(num1);
      //		System.out.println(in1.toString());
      //		
      //		Integer in2 = new Integer("123");
      //		System.out.println(in2.toString());
      //		
      //		//报异常
      		Integer in3 = new Integer("123abc");
      		System.out.println(in3.toString());
      //		
      //		Float f1 = new Float(12.3f);
      //		Float f2 = new Float("12.3");
      //		System.out.println(f1);
      //		System.out.println(f2);
      //		
      //		Boolean b1 = new Boolean(true);
      //		Boolean b2 = new Boolean("TrUe");
      //		System.out.println(b2);//true
      //		Boolean b3 = new Boolean("true123");
      //		System.out.println(b3);//false
      //		Order order = new Order();System.out.println(order.isMale);//false//boolean的默认值为falseSystem.out.println(order.isFemale);//null//类的默认值为null}}class Order{boolean isMale;Boolean isFemale;
      }
      

面试题:

  1. 如下两个题目输出结果相同吗?各是什么?

    import org.junit.Test;public class InterviewTest {@Testpublic void test1() {Object o1 = true ? new Integer(1) : new Double(2.0);System.out.println(o1);// 1.0}@Testpublic void test2() {Object o2;if (true)o2 = new Integer(1);elseo2 = new Double(2.0);System.out.println(o2);// 1}
    

    不同。
    test1()中使用到了三元运算符,其在编译时会对表达式1和表达式2的类型进行统一,因此new Integer(1) 被自动类型提升为double类型的1.0。
    test2()为if-else结构,不会进行自动类型提升。

  2. 判断输出结果。

    import org.junit.Test;@Testpublic void test3() {Integer i = new Integer(1);Integer j = new Integer(1);System.out.println(i == j);//false//引用类型的变量使用==比较的是地址值//Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],//保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在//-128~127范围内时,可以直接使用数组中的元素,不用再去new了。目的:提高效率Integer m = 1;Integer n = 1;System.out.println(m == n);//trueInteger x = 128;//相当于new了一个Integer对象Integer y = 128;//相当于new了一个Integer对象System.out.println(x == y);//false}}
    

    ① 对于i == j的判断:
    使用new即为在堆空间中创建新的对象,因此地址值不同。
    ② 对于m == n的判断:
    Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在-128~127范围内时,可以直接使用数组中的元素,不用再去new了(目的:提高效率)。此时不论新创建几个对象,其指向的内存地址值是相同的。
    ③ 对于x == y的判断:
    IntegerCache中定义的Integer[]数组范围为-128~127,而128在范围外,因此不会收到影响。在创建对象时不论是使用new还是自动装箱的方式都需要在堆空间中创建新的对象,因此地址值不同。


  • 练习题:
    利用Vector代替数组处理:从键盘读入学生成绩(以负数代表输入结束),找出最高分,并输出学生成绩等级。
    • 提示:数组一旦创建,长度就固定不变,所以在创建数组前就需要知道它的长度。而向量类java.util.Vector可以根据需要动态伸缩。
    • 创建Vector对象:Vector v=new Vector();
    • 给向量添加元素:v.addElement(Object obj); //obj必须是对象
    • 取出向量中的元素:Object obj=v.elementAt(0);
      注意第一个元素的下标是0,返回值是Object类型的。
    • 计算向量的长度:v.size();
    • 若与最高分相差10分内:A等;20分内:B等;30分内:C等;其它:D等
    package com.atguigu.exer4;import java.util.Scanner;
    import java.util.Vector;/**  利用Vector代替数组处理:从键盘读入学生成绩(以负数代表输入结束),找出最高分,并输出学生成绩等级。提示:数组一旦创建,长度就固定不变,所以在创建数组前就需要知道它的长度。而向量类java.util.Vector可以根据需要动态伸缩。创建Vector对象:Vector v=new Vector();给向量添加元素:v.addElement(Object obj);   //obj必须是对象取出向量中的元素:Object  obj=v.elementAt(0);注意第一个元素的下标是0,返回值是Object类型的。计算向量的长度:v.size();若与最高分相差10分内:A等;20分内:B等;30分内:C等;其它:D等* */
    public class ScoreTest {public static void main(String[] args) {//1. 实例化Scanner,用于从键盘获取学生成绩Scanner scan = new Scanner(System.in);//2. 创建Vector对象:Vector v=new Vector();相当于原来的数组Vector v = new Vector();//3.通过for(;;)或while(true)方式,给Vector中添加数组int maxScore = 0;while(true) {System.out.println("请输入学生的成绩(以负数代表输入结束)");int score = scan.nextInt();//3.2 当输入是负数时,跳出循环if(score < 0) {System.out.println("输入结束");break;}if(score > 100) {System.out.println("输入的数据非法,请重新输入!");continue;}//3.1 添加操作:v.addElement(Object obj)//jdk5.0之前:
    //			Integer inScore = new Integer(score);
    //			v.addElement(inScore);//多态//jdk5.0之后:v.addElement(score);//自动装箱 + 多态//4.获取学生成绩的最大值if(maxScore < score) {maxScore = score;}}//5.遍历Vector,得到每个学生的成绩,并与最大成绩比较,得到每个学生的等级。char level;for(int i = 0;i < v.size();i++){Object obj = v.elementAt(i);//jdk 5.0之前:
    //			Integer inScore = (Integer)obj;
    //			int score = inScore.intValue();//jdk 5.0之后:int score = (int)obj;if(maxScore - score <= 10){level = 'A';}else if(maxScore - score <= 20){level = 'B';}else if(maxScore - score <= 30){level = 'C';}else{level = 'D';}System.out.println("student-" + i + " score is " + score + ",level is " + level);}}
    }
    

http://chatgpt.dhexx.cn/article/j2QKUQLx.shtml

相关文章

“为了对电脑进行保护,已经阻止此应用。”

环境&#xff1a;Win10专业版 解决方法&#xff1a;

win10运行安装软件程序提示“为了对电脑进行保护,已经阻止此应用” 有效解决方法记录

问题&#xff1a; 解决&#xff1a; 在程序文件上&#xff0c;按住【shift】键&#xff0c;同时点击鼠标右键→【复制为路径】&#xff0c;打开C:\Windows\System32&#xff0c;找到cmd.exe&#xff0c;右键→【以管理员身份运行】&#xff0c;在打开的界面将路径粘贴&#xff…

【windows】Windows10为了对电脑进行保护,已经阻止此应用解决方案

winr打开运行输入“gpedit.msc”,打开“本地组策略编辑器”&#xff0c;依次点开&#xff1a; 1.计算机配置 2.Windows设置 3.安全设置 4.本地策略 5.安全选项 在右边框内找到并双击“用户账户控制&#xff1a;以管理员批准模式运行所有管理员” 在打开的对话框中选择“…

为了对电脑进行保护,已经阻止此应用 解决办法

上面就是问题&#xff0c;这个是我在安装百度硬盘搜索的时候出现的&#xff0c;不知道啥原因。在网上找了一堆的解决方法都没有成功&#xff0c;比如修改本地策略&#xff0c;修改安全级别等等。最后发现一个人的方法有效分享给大家&#xff1a; 1.用管理员身份执行cmd 2.复制该…

Win10提示“为了对电脑进行保护,已经阻止此应用”如何解决

Win10提示“为了对电脑进行保护,已经阻止此应用”如何解决 从Win7系统升级到Win10正式版系统后&#xff0c;发现其他目录的一些程序都无法打开&#xff0c;且提示“为了对电脑进行保护&#xff0c;已经阻止此应用”&#xff0c;这是怎么回事呢&#xff1f;其实&#xff0c;该问…

Win10家庭版安装软件时提示“为了对电脑进行保护,已经阻止此应用”

如果你遇到了这个问题 你也是Win10家庭版&#xff0c;则下面的方法拯救不了你&#xff1a; 第一步&#xff1a; 恢复组策略 在桌面新建文本文档&#xff0c;粘贴代码&#xff1a; echo off pushd “%~dp0” dir /b C:\Windows\servicing\Packages\Microsoft-Windows-Group…

为了保护您的计算机,windows已经关闭了此程序,电脑Win7升级Win10系统后运行程序提示为了对电脑进行保护已经阻止此应用的解决方法...

随着我国互联网技术不断的进步,计算机网络的发展也逐渐趋向成熟。计算机网络发展为人民群众生活带来了很大的益处,使得人们的生活更便捷与信息化。下面是学习啦小编为大家整理的关于电脑Win7升级Win10系统后运行程序提示为了对电脑进行保护已经阻止此应用的解决方法&#xff0c…

2021-01-29-Windows10系统专业版-右键计算机管理,弹出警告框:为了对电脑进行保护,已阻止此应用。 管理员已阻止你运行此应用。有关详细信息,请与管理员联系。完美解决方案

1、按windows按钮&#xff0c;输入cmd&#xff0c;有一个以管理员身份运行的命令&#xff0c;点击打开 1.1、如果找不到&#xff0c;可以试试这个路径&#xff1a;C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\System Tools&#xff0c;里面…

你的账户配置为阻止使用计算机,Win10提示“为了对电脑进行保护,已经阻止此应用”如何解决-系统城...

从Win7系统升级到Win10正式版系统后&#xff0c;发现其他目录的一些程序都无法打开&#xff0c;且提示“为了对电脑进行保护&#xff0c;已经阻止此应用”&#xff0c;这是怎么回事呢&#xff1f;其实&#xff0c;该问题是由于系统所登录的账号管理权限不足导致的。下面&#x…

你的账户配置为阻止使用计算机,win10运行程序提示“为了对电脑进行保护,已经阻止此应用”怎么办...

升级win10系统之后&#xff0c;有用户在安装软件程序时提示“为了对电脑进行保护&#xff0c;已经阻止此应用”&#xff0c;“管理员已阻止你运行此应用。发布者&#xff1a;不受信任”&#xff0c;这是怎么回事呢&#xff1f;其实这是win10系统的帐户安全控制机制&#xff0c;…

win10运行程序提示“为了对电脑进行保护,已经阻止此应用” 解决方法

升级win10系统之后&#xff0c;有用户在安装软件程序时提示“为了对电脑进行保护&#xff0c;已经阻止此应用”&#xff0c;“管理员已阻止你运行此应用。发布者&#xff1a;不受信任”&#xff0c;这是怎么回事呢&#xff1f;其实这是win10系统的帐户安全控制机制&#xff0c;…

为了对计算机的保护你的管理员已阻止你运行此程序,Win 10系统提示:为了对电脑进行保护 已经阻止此应用 怎么办?...

Win10 系统运行某个软件时提示“用户账号控制”&#xff0c;“为了对电脑进行保护&#xff0c;已经阻止此应用”&#xff0c;“管理员已阻止你运行此应用。有关详细信息&#xff0c;请于管理员联系”。 之前也碰到几次也按照网上的方法启用“用户账户控制&#xff1a;以管理员批…

你的账户配置为阻止使用计算机,Win10打开软件提示为了对电脑进行保护,已经阻止此应用解决方法...

近期有网友遇到一个问题&#xff0c;那就是在Windows10系统中打开或者安装软件的时候用户账户控制提示了“为了对电脑进行保护&#xff0c;已经阻止此应用”的问题&#xff0c;不知道如何解决该问题。针对这个问题&#xff0c;装机之家分享了Win10打开软件提示&#xff02;为了…

Window异常提示:“为了对电脑进行保护,已经阻止此应用”

目录 1.Window异常提示图片&#xff1a; 2.家庭版额外需要执行的解决方式&#xff1a; 3.本地组策略编辑器(后续家庭版和专业版一致) 4.禁用后重新启动电脑就可以正常运行程序了 1.Window异常提示图片&#xff1a; 2.家庭版额外需要执行的解决方式&#xff1a; 代码&#xff…

win10 安装程序显示“为了对电脑进行保护,已经阻止此应用” 管理员已阻止你运行此应用

现象如下图所示&#xff1a; 解决方法如下&#xff1a; 1、“winx”进入控制面板&#xff0c;选择安全性与维护&#xff0c;在左侧更改windows smartscreen筛选器设置&#xff0c;选择"不执行任何操作"&#xff0c;单击确定即可。 2、“winx”进入控制面板&#xff0…

Windows10安装打开软件提示“为了对电脑进行保护,已经阻止此应用”解决方法!

下载好了却安装不上&#xff1f; 在Windows10系统中打开或者安装部分软件的时候用户账户控制提示了“为了对电脑进行保护&#xff0c;已经阻止此应用”。 解决方法 1.按下WinR“”组合快捷键打开“运行”&#xff0c;在运行中输入“gpedit.msc”并确定即可打开组策略编辑器。…

windows提示“为了对电脑进行保护,已经阻止此应用mmc.exe ”

1、mmc.exe无法运行&#xff0c;可能会遇到在运行中输入“gpedit.msc”命令无法打开本地组策略编辑器&#xff0c;所以我们鼠标右键点击“开始菜单”&#xff0c;点击“windowspowershell&#xff08;管理员&#xff09;”&#xff0c;或者是开始→window系统→命令提示符→右击…

正确解决Win10提示管理员已阻止你运行此应用mmc.exe

最近某网友遇到了个问题。win10系统下&#xff0c;右击此电脑&#xff0c;点管理时&#xff1b;或者右击开始按钮&#xff0c;点计算机管理时&#xff08;或设备管理器&#xff09;&#xff0c;会弹出警告提示&#xff1a; 用户账户控制 为了对电脑进行保护&#xff0c;已阻止此…

用户帐户控制---为了对电脑进行保护,已经阻止此应用。---管理员已阻止你运行此应。有关详细信息,请与管理员联系。

最近莫名其妙想打开设备管理器的时候就突然跳出这个提示 上网搜索大多数的解决办法都是说 ①用户账户控制设置为“从不通知”&#xff1b; ②或者在windows防火墙设置“允许应用或功能通过Windows Defender防火墙”中添加mmc.exe; ③还有建议打开本地策略组进行设置的&#xf…

安装软件一直报错:为了对电脑进行保护,已经阻止此应用。管理员已阻止你运行此应用。有关详情信息,请与管理员联系。

安装Tableau 2020 一直报错&#xff1a;为了对电脑进行保护&#xff0c;已经阻止此应用。管理员已阻止你运行此应用。有关详情信息&#xff0c;请与管理员联系。 解决方法 &#xff1a; 以管理员身份打开cmd 用 cd 命令一直打开到运行程序所在的文件夹路径 比如说我的Tableau运…