一,简答题
-
解释this关键字的作用和用法?
作用:this在类中用来表示当前正在访问的对象,(this.成员变量名)--显示的访问当前对象的成员变量 用法: public class thisdemo { String name; int age; String gender; /* this关键字代表当前对象 使用this关键字引用成员变量 使用this关键字引用成员方法或构造方法。 */ public thisdemo(){ //在类中的某个构造方法中,使用this关键字调用另一个构造方法 this("小明",22,"男"); } //this在类中用来表示当前正在访问的对象,this.成员变量--显示的访问当前对象的成员变量 public thisdemo(String name,int age,String gender){ this.age=age; this.name=name; this.gender=gender; } public void showinfo(){ System.out.println("姓名:"+name+" 年龄:"+age+" 性别:"+gender); } public void work(){ System.out.println(""); } } public class thistest { public static void main(String[] args) { //调用无参构造方法初始化对象,然后给对象的属性赋值 /* Person pl =new Person(); p1.name ="张三"; p1.age =20; p1.gender ="男"; p1.showInfo();*/ thisdemo p1=new thisdemo("李四",22,"男"); p1.showinfo(); thisdemo p2=new thisdemo("小红",18,"女"); p2.showinfo(); } }
-
静态成员变量与非静态成员变量的区别?
static--静态 可以修饰类中的成员变量,成员方法,代码块,内部类。 static的修饰属性(成员变量) static关键字修饰属性的特点; 静态成员变量也称为类变量,在内存中只有一份,所有对象可以共享, 一般情况下,将类中所有对象都相同的属性设置为静态的, static修饰内容,随着类的加载而加载,类只要被加载了就可以直接通过类名访问 优先于对象存在 静态成员被被所有的对象共享 可以直接使用类名访问(建议) static修饰成员方法,修饰的成员方法也称为类方法,可以直接使用类名访问.在静态的方法中只能访问静态的成员变量 非静态的方法中可以使用静态的成员变量 非静态方法可以使用静态的成员变量 eg: package stict; //static 固定国籍中国; public class staticdemo { String name;//中国人,但每个人的姓名会有不同,所以需要在每个对象中都加入名字这个属性 static String country="中国"; //使用一个变量记录一次,最终得出此类被创在了多少的对象; static int count=0; //利用这个方法我们可以实现每次调用count这个方法时,实现加1达到表示次数的目的 public staticdemo (){ count=count+1; } /* static 修饰的方法,称为静态方法,可以直接使用类名调用 静态的方法中,只能使用静态的成员变量,因为他们都是随着类的加载而加载的 */ public static void test(){ System.out.println(count); } //一旦方法中,使用了非成员变量,则此方法就不可以定义为静态成员变量 //换个意思就是一旦方法是静态的,其内部就不可以包含有对象成员。 public void eat(){ System.out.println(name+"吃饭"); System.out.println(count);//但是非静态方法中可以使用静态成员变量 } } package stict; public class statictext { public static void main(String[] args) { //静态成员随着类的加载而加载的,只要类加载了,静态成员就可以直接被使用了 System.out.println(staticdemo.country); //由于不管是何逸泽还是小明都是中国人,所以在每次创造对象是就不需要再说明是国籍中国 //只需要static固定国籍就行 staticdemo s1=new staticdemo(); s1.name="何逸泽"; s1.eat(); staticdemo s2=new staticdemo(); s2.name="小明"; s2.eat(); System.out.println(s1.name); System.out.println(s2.name); System.out.println("==============================="); System.out.println(staticdemo.country);
-
什么是代码块,代码块有哪几种,各自的特征?
package stict; public class staticdemo { /* 代码块; 在类中声明一个没有名字的代码块; 分为实例代码块和静态代码块两种 实例代码块;每次创建对象时执行 静态代码块:在类被加载时自动执行 */ static int num=1235;//因为要用在静态代码块里 所以需要我们用静态static来修饰num //实例代码块 { System.out.println("我是实例代码块,在创建对象时,不需要显示的调用,我会自动执行1"); } { System.out.println("我是实例代码块,在创建对象时,不需要显示的调用,我会自动执行2"); } //静态代码块 static { System.out.println("我是静态代码块,在类被加载时,会自动执行"); } } package stict; public class statictext { public static void main(String[] args) { //静态成员随着类的加载而加载的,只要类加载了,静态成员就可以直接被使用了 System.out.println(staticdemo.country); System.out.println(staticdemo.num); } }
-
什么是包,包的作用有哪些?
● 在编写 Java 程序时,随着程序架构越来越大,类的个数也越来越多,这时就会 发现管理程序中维护类名称也是一件很麻烦的事,尤其是一些同名问题的发生。 有时,开发人员还可能需要将处理同一方面的问题的类放在同一个目录下,以 便于管理。 ● 为了解决上述问题,Java 引入了包(package)机制,提供了类的多层命名空 间,用于解决类的命名冲突、类管理等问题。 ● 使用package关键字修饰包 ● 每个类属于一个特定的包,所以java中一个类的完整名称等于包名+类名 ● 包的作用: 按照不同功能管理类 避免类重名 控制访问权限
-
访问权限修饰符有哪些?
package com.day2.demo1; import java.util.Arrays; //权限修饰符; public class Animal { public String pubname;//公共权限修饰成员,在任何地方都可以访问到,本类,本包其他类,其他包中的类; protected String proname;//受保护权限,在本类中,同包的其他类中可以访问到,还可以在其他包的子类访问; String name; //默认权限 在本类中,同包的其他类中可以访问到 private String priname; //私有权限 在本类中访问到 public void pubtest(){ System.out.println("========================="); } public static void main(String[] args) { Animal animal=new Animal(); animal.pubname="1111";//证明公共权限修饰的成员可以在本类中访问到; animal.proname="2222";//证明受保护的权限成员可以在本类中访问到; animal.name="3333";//证明默认权限成员可以在本类中访问到; animal.priname="4444";//证明私有权限可以在本类中访问到; animal.pubtest(); } //公共 受保护 默认 私有都可以修饰内部; }
二,编程题
1.
设计一个简单的计算器类
可以对两位整数(int类型)进行的加、减、乘、除运算,并返回运算结果
在main方法中调用测试
package com.day2; public class work1 { /* 设计一个简单的计算器类 可以对两位整数(int类型)进行的加、减、乘、除运算,并返回运算结果 在main方法中调用测试 */ static int addition; static int subtraction; static int multiplication; static int division; public static int addition(int a,int b){ addition=a+b; return addition; } public static int subtraction(int b,int a){ if(a>b){ subtraction=a-b; }else{ subtraction=b-a; } return subtraction; } public static int multiplication(int a,int b){ multiplication=a*b; return multiplication; } public static int division(int a,int b){ division=a/b; return division; } } package com.day2; public class work1text { public static void main(String[] args) { System.out.println(work1.addition(1,1));//加法 System.out.println(work1.subtraction(10,5));//减法 System.out.println(work1.multiplication(2,3));//乘法 System.out.println(work1.division(6,2));//除法 } }
2声明一个银行账户类Account
成员变量:利率、账号、余额,其中所有账户的利率是相同的
提供有参构造public Account(String account,double balance)
提供计算年利息方法public double annualInterest(),余额*利率
在测试类的main中,创建账户类的两个对象,账号分别为“11111”和“22222”,余额分别为“1000和2000”元,利率值是0.035,调用方法计算两个账号的利息。
package com.day2; /* 2声明一个银行账户类Account 成员变量:利率、账号、余额,其中所有账户的利率是相同的 提供有参构造public Account(String account,double balance) 提供计算年利息方法public double annualInterest(),余额*利率 在测试类的main中,创建账户类的两个对象,账号分别为“11111”和“22222”,余额分别为“1000和2000”元,利率值是0.035,调用方法计算两个账号的利息。 */ public class work2 { static double interestRate=0.035;//利率 static String account;//账号 static double balance;//余额 static double interests;//利息 public static double annualInterest(String account,double balance){ account= account; interests=balance*interestRate; return interests; } public void Account(String account,double balance) { this.account = account; this.balance=balance; } } package com.day2; public class work2text { public static void main(String[] args) { System.out.println("利率是:"+work2.interestRate); System.out.println("1111的利息是"+work2.annualInterest("1111",1000)); System.out.println("1111的利息是"+work2.annualInterest("2222",2000)); } }
3求三角形面积
1.声明一个图形工具类GraphicTools,包含两个重载方法
方法1:public double getArea(double base, double height),根据底边和高,求三角形面积,
方法2:public double getArea(double a, double b, double c),根据三条边,求三角形面积,根据三角形三边求面积的海伦公式:
-
在测试类的main方法中调用(求平方根使用Math类中的sqrt方法,用法Math.sqrt(值))
package com.day2; /* 3求三角形面积 1.声明一个图形工具类GraphicTools,包含两个重载方法 方法1:public double getArea(double base, double height),根据底边和高,求三角形面积, 方法2:public double getArea(double a, double b, double c),根据三条边,求三角形面积,根据三角形三边求面积的海伦公式: 2. 在测试类的main方法中调用(求平方根使用Math类中的sqrt方法,用法Math.sqrt(值)) */ public class GraphicTools { double base;//底 double height;//高 static double area;//面积 public static double getArea(double base, double height){ area=(base*height)/2;//底乘高除2 return area; } public static double getArea(double a, double b, double c){ double p=(a+b+c)/2; double d=p*(p-a)*(p-b)*(p-c); double n=Math.sqrt(d); return n; } } package com.day2; public class GraphicToolstest { public static void main(String[] args) { System.out.println(GraphicTools.getArea(10,25)); System.out.println(GraphicTools.getArea(3,4,5)); } }