interface Com { int add(int a,int b); } abstract class A { abstract int add(int a,int b); } class B extends A implements Com{ public int add(int a,int b) { return a+b; } } public class E { public static void main(String args[]) { B b = new B(); Com com = b; System.out.println(com.add(12,6)); //【代码1】 A a = b; System.out.println(a.add(10,5)); //【代码2】 } }
时间: 2024-04-04 13:07:19 浏览: 91
这段代码是一个 Java 程序,定义了一个接口 Com 和一个抽象类 A,类 B 继承了 A 并且实现了 Com 接口。在 main 方法中,通过创建 B 类的实例 b,将其分别强制转换为 Com 接口类型的实例 com 和 A 抽象类类型的实例 a。然后分别调用 com.add(12,6) 和 a.add(10,5) 方法,输出它们的返回值。
在代码1中,使用了接口类型的引用 com 来调用 add 方法,输出 18。
在代码2中,使用了抽象类类型的引用 a 来调用 add 方法,输出 15。这是因为类 B 继承了抽象类 A,并且实现了 add 方法,所以可以通过 A 的引用来调用子类 B 的 add 方法。
相关问题
根据以下要求写出java代码:and a Shape class that implements IShape and has the following UML specification: +------------------------------------+ | Shape | +------------------------------------+ | - x: int | | - y: int | | - color: Color | +------------------------------------+ | + Shape(int x, int y) | | + getX(): int | | + getY(): int | | + setX(int x): void | | + setY(int y): void | | + isVisible(int w, int h): boolean | | + isIn(int x, int y): boolean | | + draw(Graphics g): void | | + testShape(): void | +------------------------------------+ The x and y instance variables indicate the position of the center of the shape, and the color instance variable indicates the color of the shape. The color of the shape is computed randomly in the constructor of the shape class and never changes after that, like this: color = new Color((float)Math.random(), (float)Math.random(), (float)Math.random()); The isVisible method is abstract, and indicates whether the shape is currently visible or not inside a window of width w and of height h. The isIn method is abstract, and indicates whether the point at coordinates (x, y) is currently inside the shape or not. The draw method simply changes the color of the graphics object g to be the correct color for the shape. Also add to your program a Start class to test your Shape class.
以下是Java代码实现:
```
import java.awt.Color;
import java.awt.Graphics;
interface IShape {
public boolean isVisible(int w, int h);
public boolean isIn(int x, int y);
}
class Shape implements IShape {
private int x;
private int y;
private Color color;
public Shape(int x, int y) {
this.x = x;
this.y = y;
this.color = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isVisible(int w, int h) {
// check if shape is within the window boundaries
if (x > 0 && x < w && y > 0 && y < h) {
return true;
}
return false;
}
public boolean isIn(int x, int y) {
// check if point is within the shape boundaries
return false;
}
public void draw(Graphics g) {
g.setColor(color);
}
public void testShape() {
// implement test cases for Shape class
}
}
public class Start {
public static void main(String[] args) {
// test Shape class here
}
}
```
use java language ,In this project you need to write a book lending system for a Library. The system has different roles for registered users. There are two types of user roles: borrower and lender. Write an IUser interface for library users, with the following UML specification: +----------------------------------+ | <<interface>> | | IUser | +----------------------------------+ | + getName(): String | | + getBook(): int | | + moreBook(int number): void | +----------------------------------+ and a User class that implements IUser and has the following UML specification: +-----------------------------------+ | User | +-----------------------------------+ | - name: String | | - book: int | +-----------------------------------+ | + User(String name, int book) | | + getName(): String | | + getBook(): int | | # setBook(int book): void | | + moreBook(int number): void | | + testUser(): void | +-----------------------------------+ The name instance variable indicates the user name. The book instance variable indicates the number of books borrowed by the user. The setBook method changes the number of books borrowed by the user. The setBook method is protected, not public. This means that only subclasses of the User class can use the setBook method. All the other classes in the system cannot use the setBook method, so they cannot change the number of books borrowed by a user. The purpose of the moreBook method is to increase the number of books borrowed or lent by the user (depending on what kind of user it is) by the number given as argument to the method. The moreBook method of the User class is abstract, since we do not know what kind of role the user is (a borrower borrows books from other users and a lender lend books to other users). Also add to your program a Test class to test your User class. public class Test { public static void main(String[] args) { User.testUser(); } }
Sure, I can help you with that! Here's the code for the IUser interface and User class:
IUser interface:
```java
public interface IUser {
String getName();
int getBook();
void moreBook(int number);
}
```
User class:
```java
public class User implements IUser {
private String name;
private int book;
public User(String name, int book) {
this.name = name;
this.book = book;
}
@Override
public String getName() {
return name;
}
@Override
public int getBook() {
return book;
}
protected void setBook(int book) {
this.book = book;
}
public void moreBook(int number) {
// abstract method, implementation will be provided in subclasses
}
public static void testUser() {
User borrower = new Borrower("John", 2);
User lender = new Lender("Jane", 5);
System.out.println("Borrower " + borrower.getName() + " has " + borrower.getBook() + " books borrowed.");
System.out.println("Lender " + lender.getName() + " has " + lender.getBook() + " books lent.");
borrower.moreBook(1);
lender.moreBook(3);
System.out.println("Borrower " + borrower.getName() + " has " + borrower.getBook() + " books borrowed.");
System.out.println("Lender " + lender.getName() + " has " + lender.getBook() + " books lent.");
}
}
```
The User class has a constructor that takes a name and a number of books as arguments. It also implements the methods of the IUser interface.
The setBook method is protected, meaning it can only be accessed by subclasses of User.
The moreBook method is abstract, as its implementation will depend on the type of user (borrower or lender). This will be implemented in the subclasses.
Here's an example of two subclasses, Borrower and Lender, that extend User and implement the moreBook method:
```java
public class Borrower extends User {
public Borrower(String name, int book) {
super(name, book);
}
@Override
public void moreBook(int number) {
setBook(getBook() + number);
}
}
public class Lender extends User {
public Lender(String name, int book) {
super(name, book);
}
@Override
public void moreBook(int number) {
setBook(getBook() - number);
}
}
```
The Borrower class increases the number of books borrowed, while the Lender class decreases the number of books lent.
And finally, here's the Test class to test the User class:
```java
public class Test {
public static void main(String[] args) {
User.testUser();
}
}
```
This will create a Borrower and a Lender object, print their initial number of books, call the moreBook method on both objects, and print the new number of books.
阅读全文