{"id":715,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java \u5bf9\u8c61\u548c\u7c7b","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/715.html","title":{"rendered":"Java \u5bf9\u8c61\u548c\u7c7b"},"content":{"rendered":"
\n
\u901a\u8fc7\u53c2\u8003\u53d8\u91cf<\/span> <\/p>\n <\/p>\n \u4f7f\u7528new\u5173\u952e\u5b57<\/span>
\n \u901a\u8fc7\u65b9\u6cd5<\/span>
\n \u901a\u8fc7\u6784\u9020\u51fd\u6570<\/span> <\/p>\n 1)\u5bf9\u8c61\u548c\u7c7b\u793a\u4f8b: \u901a\u8fc7\u5f15\u7528\u8fdb\u884c\u521d\u59cb\u5316<\/h3>\n
class <\/span>Student{
int <\/span>id;
String name;
}
class <\/span>TestStudent2{
public <\/span>static void <\/span>main(String args[]){
Student s1=new Student();
s1.id=101;
s1.name=\"Sonoo\"<\/span>;
System.out.println<\/span>(s1.id+\" \"<\/span>+s1.name);
\/\/printing members with a white space }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 101 Sonoo
<\/span><\/code><\/pre>\n<\/p><\/div>\n class <\/span>Student{
int <\/span>id;
String name;
}
class <\/span>TestStudent3{
public <\/span>static void <\/span>main(String args[]){
\/\/Creating objects Student s1=new Student();
Student s2=new Student();
\/\/Initializing objects s1.id=101;
s1.name=\"Sonoo\"<\/span>;
s2.id=102;
s2.name=\"Amit\"<\/span>;
\/\/Printing data System.out.println<\/span>(s1.id+\" \"<\/span><\/span>+s1.name);
System.out.println<\/span>(s2.id+\" \"+s2.name);
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 101 Sonoo102 Amit
<\/span><\/code><\/pre>\n<\/p><\/div>\n 2)\u5bf9\u8c61\u548c\u7c7b\u793a\u4f8b: \u901a\u8fc7\u65b9\u6cd5\u8fdb\u884c\u521d\u59cb\u5316<\/h3>\n
class <\/span>Student{
int <\/span>rollno;
String name;
void insertRecord(int <\/span>r, String n){
rollno=r;
name=n;
}
void displayInformation(){
System.out.println<\/span>(rollno+\" \"<\/span>+name);
}
}
class <\/span>TestStudent4{
public <\/span>static void <\/span>main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord<\/span>(111,\"Karan\"<\/span>);
s2.insertRecord<\/span>(222,\"Aryan\"<\/span>);
s1.displayInformation<\/span>();
s2.displayInformation<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 111 Karan222 Aryan
<\/span><\/code><\/pre>\n<\/p><\/div>\n <\/p>\n
\n 3)\u5bf9\u8c61\u548c\u7c7b\u793a\u4f8b: \u901a\u8fc7\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316<\/h3>\n
\n\u5bf9\u8c61\u548c\u7c7b\u793a\u4f8b: \u5458\u5de5<\/h3>\n
class <\/span>Employee{
int <\/span>id;
String name;
float<\/span> salary;
void insert(int <\/span>i, String n, float<\/span> s) {
id=i;
name=n;
salary=s;
}
void display(){
System.out.println<\/span>(id+\" \"<\/span><\/span>+name+\" \"+salary);
}
}
public <\/span>class <\/span>TestEmployee {
public <\/span>static void <\/span>main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
e1.insert<\/span>(101,\"ajeet\"<\/span>,45000);
e2.insert<\/span>(102,\"irfan\"<\/span>,25000);
e3.insert<\/span>(103,\"nakul\"<\/span>,55000);
e1.display<\/span>();
e2.display<\/span>();
e3.display<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 101 ajeet 45000.0102 irfan 25000.0103 nakul 55000.0
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5bf9\u8c61\u548c\u7c7b\u793a\u4f8b: Rectangle<\/h3>\n
class <\/span>Rectangle{
int <\/span>length;
int <\/span>width;
void insert(int <\/span>l, int <\/span>w){
length=l;
width=w;
}
void calculateArea(){
System.out.println<\/span>(length*width);
}
}
class <\/span>TestRectangle1{
public <\/span>static void <\/span>main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert<\/span>(11,5);
r2.insert<\/span>(3,15);
r1.calculateArea<\/span>();
r2.calculateArea<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 55 45
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5728Java\u4e2d\u521b\u5efa\u5bf9\u8c61\u7684\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/h2>\n
\n \u901a\u8fc7newInstance()\u65b9\u6cd5<\/span>
\n \u901a\u8fc7clone()\u65b9\u6cd5<\/span>
\n \u901a\u8fc7\u53cd\u5e8f\u5217\u5316<\/span>
\n \u901a\u8fc7\u5de5\u5382\u65b9\u6cd5\u7b49<\/span> <\/p>\n
\n <\/p>\n\u533f\u540d\u5bf9\u8c61<\/h2>\n
new Calculation();
\/\/anonymous object
<\/span><\/code><\/pre>\n<\/p><\/div>\n Calculation c=new Calculation();
c.fact<\/span>(5);
<\/span><\/code><\/pre>\n<\/p><\/div>\n new Calculation().fact<\/span>(5);
<\/span><\/code><\/pre>\n<\/p><\/div>\n class <\/span>Calculation{
void fact(int <\/span> n){
int <\/span>fact=1;
for(int <\/span>i=1;i<=n;i++){
fact=fact*i;
}
System.out.println<\/span>(\"factorial is \"<\/span>+fact);
}
public <\/span>static void <\/span>main(String args[]){
new Calculation().fact<\/span>(5);
\/\/calling method with anonymous object}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n Factorial is 120
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u4ec5\u6309\u4e00\u79cd\u7c7b\u578b\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61<\/h3>\n
int <\/span>a=10, b=20;
<\/span><\/code><\/pre>\n<\/p><\/div>\n Rectangle r1=new Rectangle(), r2=new Rectangle();
\/\/creating two objects
<\/span><\/code><\/pre>\n<\/p><\/div>\n \/\/Java Program to illustrate the use of Rectangle class <\/span>which
\/\/has length and width data membersclass <\/span>Rectangle{
int <\/span>length;
int <\/span>width;
void insert(int <\/span>l,int <\/span>w){
length=l;
width=w;
}
void calculateArea(){
System.out.println<\/span>(length*width);
}
}
class <\/span>TestRectangle2{
public <\/span>static void <\/span>main(String args[]){
Rectangle r1=new Rectangle(),r2=new Rectangle();
\/\/creating two objects r1.insert<\/span>(11,5);
r2.insert<\/span>(3,15);
r1.calculateArea<\/span>();
r2.calculateArea<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 55 45
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u771f\u5b9e\u793a\u4f8b: TestAccount<\/h3>\n
\/\/Java Program to demonstrate the working of a banking-system
\/\/where we deposit and withdraw amount from our account.
\/\/Creating an Account class <\/span>which has deposit() and withdraw() methodsclass <\/span>Account{
int <\/span>acc_no;
String name;
float<\/span> amount;
\/\/Method to initialize objectvoid insert(int <\/span>a,String n,float<\/span> amt){
acc_no=a;
name=n;
amount=amt;
}
\/\/deposit methodvoid deposit(float<\/span> amt){
amount=amount+amt;
System.out.println<\/span>(amt+\" deposited\"<\/span>);
}
\/\/withdraw methodvoid withdraw(float<\/span> amt){
if(amount<amt){
System.out.println<\/span>(\"Insufficient Balance\"<\/span>);
}
else{
amount=amount-amt;
System.out.println<\/span>(amt+\" withdrawn\"<\/span>);
}
}
\/\/method to check the balance of the accountvoid checkBalance(){
System.out.println<\/span>(\"Balance is: \"<\/span>+amount);
}
\/\/method to display the values of an objectvoid display(){
System.out.println<\/span>(acc_no+\" \"<\/span><\/span>+name+\" \"+amount);
}
}
\/\/Creating a test class <\/span>to deposit and withdraw amountclass <\/span>TestAccount{
public <\/span>static void <\/span>main(String[] args){
Account a1=new Account();
a1.insert<\/span>(832345,\"Ankit\"<\/span>,1000);
a1.display<\/span>();
a1.checkBalance<\/span>();
a1.deposit<\/span>(40000);
a1.checkBalance<\/span>();