Sharing staging or agent between multiple jobs

I’ve several jobs which are defined in global library and I want to share some stage and agent between jobs.
For example:

class share implements Serializable {//share.groovy
def defaultAgent(context){
context.with {
kubernetes {…}

//the jobs
void call(parameters) { //job1.groovy
pipeline {

but when run the job I got the following error:
Invalid agent type “defaultAgent” specified. Must be one of [any, docker, dockerfile, kubernetes, label, none]

Is it possible to share code between job in libary? How can this be done?