private static final 之间可以任意结合
abstract 不可以与private static final 结合使用
A. static 可以修饰所有类型的变量???不可以修饰局部变量,局部变量作用范围小,不是类变量
B. static 可以修饰方法?
C. final 可以修饰类
D. abstract 可以修饰类、方法、属性??不能修饰属性
A. static 修饰的属性称为静态变量,被所有的对象共享
B. static 修饰的方法,可以直接用 类名.方法名(参数)调用
C. static 修饰的方法中可以直接访问本类的其他方法????非静态元素不能被静态元素调用
D. 父类中定义了 static 修饰的方法,子类中不能再定义同名的 static 方法
可以定义但是不是重写,多态中父类对象调用的还是父类中的方法
A. final 修饰的类是作用范围内常量,只允许一次赋值???作用范围是地址,地址不可更改,内容可以改
B. final 修饰的方法不能被继承???可以被继承,但不能被重写
C. final 修饰的方法不允许被覆盖
D. final 修饰的类没有子类
A. abstract 类中可以没有 abstract 方法
B. abstract 类的子类也可以是 abstract 类
C. abstract 类不能创建对象,但可以声明引用
D. abstract 方法不能有方法体
300 200 300 400
A. //1 ?B. //2 C. //3 ?D. //4
E. //5 ?F. //6 G. //7 ?H. //8
In Static
MyClass() ?
10
MyClass(int)
10
A. 编译通过,输出 10
B. 编译不通过,把第 2 行改为 final int value = 10;
C. 编译不通过,把第 3 行改为 public MyClass(){ value = 10; }
D. 以上描述都不正确
A. 编译不通过
B. 编译通过;在//1 处加上:mv.value = 200; 则编译不通过
C. 编译通过。如果在//1 处加上:mv = new MyValue(); 则编译不通过
D. 以上描述都不正确
A. 编译通过? B. //1 出错 C. //2 出错
D. //3 出错 E. //4 出错
可以
m1() ?in super
m1(int) ?in ?sub
m1(double) ?in sub
m1 in Super
m2 in Sub
m1 in Sub
m2 in Sub
A. abstract final void m()
B. public void final m()
C. static abstract void m()
D. private abstract void m()
E. public static final void m()
(1) abstract 可以修饰类、方法、属性(F)
(2) 抽象类中不一定有构造方法(F)有,但是不能实例化
(3) 抽象类只能声明引用,不允许单独 new 对象(T)
(4) 所有类中都可以定义抽象方法(F)抽象方法必须在抽象类中
(5) 子类继承抽象类必须实现抽象类中所有抽象方法(T)
(6) static 可以修饰属性、方法、代码块(T)
(7) 静态属性只能用 类名.静态属性名 访问 (T)
(8) 静态方法可以被本类中的其他方法直接访问(T)
(9) 静态代码块在创建对象时完成静态属性的初始化工作(F)类加载时
(10)final 只能修饰属性、方法、类(F)??局部变量
(11)final 修饰的方法不能被继承(F)
(12)final 修饰的类不能被继承,即没有子类(T)
package com.by.homework3.figure;
public abstract class Figure {
????public abstract void qiuzhouchang();
????public abstract void qiumianji();
}
package com.by.homework3.figure;
public class Circle extends Figure{
????int banjin;
????public Circle(int banjin) {
????????this.banjin = banjin;
????}
????@Override
????public void qiuzhouchang() {
????????Double zhouchang=2*3.14*banjin;
????????System.out.println("圆的周长:"+zhouchang);
????}
????@Override
????public void qiumianji() {
????????Double mianji=3.14*banjin*banjin;
????????System.out.println("圆的面积:"+mianji);
????}
}
package com.by.homework3.figure;
import com.by.homework3.figure.Figure;
public class Rect extends Figure {
?????int chang;
?????int kuan;
????public Rect() {
????}
????public Rect(int chang,int kuan) {
????????this.chang = chang;
????????this.kuan=kuan;
????}
????@Override
????public void qiuzhouchang() {
????????int zhouchang=chang*2+kuan*2;
????????System.out.println("矩形的周长为:"+zhouchang);
????}
????@Override
????public void qiumianji() {
????????int mianji=chang*kuan;
????????System.out.println("矩形面积为:"+mianji);
????}
}
package com.by.homework3.figure;
public class Square extends Figure{
????int bianchang;
????public Square(){
????}
????public Square(int bianchang) {
????????this.bianchang = bianchang;
????}
????public void qiuzhouchang(){
????????int zhouchang=bianchang*4;
????????System.out.println("正方形的周长:"+zhouchang);
????}
????public void qiumianji(){
????????int mianji=bianchang*bianchang;
????????System.out.println("正方形的面积:"+mianji);
????}
}
定义一个静态的count属性,无论哪个对象调用这个属性,静态元素只看最后改的那个数
package com.by.homework5;
class MyClass {
????public static int count=0;
????public MyClass(){
????????count++;
????}
}
public class MyClassTest{
????public static void main(String[]args){
????????MyClass myClass=new MyClass();
????????MyClass myClass1=new MyClass();
????????MyClass myClass2=new MyClass();
????????MyClass myClass3=new MyClass();
????????System.out.println(myClass3.count);
????}
}
package com.by.homework5;
abstract class Worker{
????private int id;
????private String name;
????public Worker() {
????}
????public Worker(int id, String name) {
????????this.id = id;
????????this.name = name;
????}
????public int getId() {
????????return id;
????}
????public void setId(int id) {
????????this.id = id;
????}
????public String getName() {
????????return name;
????}
????public void setName(String name) {
????????this.name = name;
????}
????public void eat(){
????????System.out.println("吃饭方法");
????}
????public abstract void work();
}
class Programmer extends Worker{
????public Programmer() {
????}
????public Programmer(int id, String name) {
????????super(id, name);
????}
????@Override
????public void eat() {
????????super.eat();
????}
????@Override
????public void work() {
????????System.out.println("写代码");
????}
}
class Teacher extends Worker{
????public Teacher() {
????}
????public Teacher(int id, String name) {
????????super(id, name);
????}
????public void work(){
????????System.out.println("讲课");
????}
}
class Management extends Worker{
????public Management() {
????????super();
????}
????public Management(int id, String name) {
????????super(id, name);
????}
????@Override
????public int getId() {
????????return super.getId();
????}
????@Override
????public void setId(int id) {
????????super.setId(id);
????}
????@Override
????public String getName() {
????????return super.getName();
????}
????@Override
????public void setName(String name) {
????????super.setName(name);
????}
????@Override
????public void eat() {
????????super.eat();
????}
????@Override
????public void work() {
????????System.out.println("拉网线");
????}
}
public class WorkerTest {
????public static void main(String[]args){
????????Worker worker=new Programmer(111,"张三");
????????System.out.println("id为"+worker.getId()+"的"+worker.getName());
????????worker.work();
????????Worker worker1=new Management(222,"李四");
????????System.out.println("id为"+worker1.getId()+"的"+worker1.getName());
????????worker1.work();
????????Worker worker2=new Teacher(333,"王五");
????????System.out.println("id为"+worker2.getId()+"的"+worker2.getName());
????????worker2.work();
????}
}
abstract ?Super{
void m();
void m2();
}
class Sub extends Super{
//1.
}
abstract Sub1 extends Super{
//2.
}
1:void m(){}
void m2(){}
2: void m();
void m2();
抽象类 Teacher,有两个方法teach(),add(int a,int b)
抽象子类 MathTeacher,继承于Teacher类,有一个方法teachMath();
第三级类 RealMathTeacher,继承于MathTeacher,有两个方法TeachHightMath(),add(int a,int b);
在主函数中,声明Teacher类引用,并且调用add方法
package com.by.homework5;
abstract class Teacher1{
????public int a;
????public int b;
????public ?void taech(){};
????public abstract void add(int a,int b);
}
abstract class MathTeacher extends Teacher1{
????public ?void teachMath(){};
}
class RealMathTeacher extends MathTeacher{
????public void teachHighMath(){
????????System.out.println("教高数");
????}
????@Override
????public void add(int a, int b) {
????????int sum=a+b;
????????System.out.println("子类add方法"+sum);
????}
}
public class TeacherTest {
????public ?static void main(String[]args){
????????Teacher1 teacher1=new RealMathTeacher();
????????RealMathTeacher r=(RealMathTeacher) teacher1;
????????r.add(3,4);
????}
}
经理也是员工,除了含有员工的属性外,另外还有一个奖金属性。
请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。
分析:
普通员工类,
变量:姓名、工号以及工资
方法:工作
经理类:
变量:姓名、工号以及工资,奖金属性
方法:工作
编写相应测试类进行测试
package com.by.homework5;
class Employer{
????private String name;
????private String id;
????private double salary;
????public Employer() {
????}
????public Employer(String name, String id, double salary) {
????????this.name = name;
????????this.id = id;
????????this.salary = salary;
????}
????public String getName() {
????????return name;
????}
????public void setName(String name) {
????????this.name = name;
????}
????public String getId() {
????????return id;
????}
????public void setId(String id) {
????????this.id = id;
????}
????public double getSalary() {
????????return salary;
????}
????public void setSalary(double salary) {
????????this.salary = salary;
????}
}
class Manager extends Employer{
????private String ?bonus;
????public Manager(){}
????public Manager(String bonus) {
????????this.bonus = bonus;
????}
????public Manager(String name, String id, double salary, String bonus) {
????????super(name, id, salary);
????????this.bonus = bonus;
????}
????public String getBonus() {
????????return bonus;
????}
????public void setBonus(String bonus) {
????????this.bonus = bonus;
????}
}
public class Employee {
????public static void main(String[]args){
????????Manager manager=new Manager("张三","123",15000,"2000");
????????System.out.println(manager.getName()+manager.getId()+manager.getSalary()+manager.getBonus());
????}
}